Created
December 1, 2014 21:12
-
-
Save evgenidb/8dfacd691669e4a9a710 to your computer and use it in GitHub Desktop.
Solution for the math expression validation problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The version I did during the class. | |
# This is only the regex. Replace it in the above code for it to work. | |
# Note: this is untested!!! Might have some bugs or missing '\s*'s | |
%r{ | |
# Named Groups declarations | |
(?<sign> | |
[-+]? | |
){0} | |
(?<int> | |
\d+ | |
(_\d*)* | |
){0} | |
(?<var> | |
[a-z] | |
){0} | |
(?<operator> | |
[-+*\/] | |
){0} | |
(?<val> | |
( | |
\g<sign>\g<int> | |
| | |
\g<sign>\g<var> | |
) | |
){0} | |
(?<basic_expression> | |
( | |
\g<val> | |
| | |
\g<expression_with_brackets> | |
) # Use only val and expression_with_brackets and not just \g<expression>. | |
# Otherwise: | |
# 1) infinite recursion; | |
# 2) All other cases should be covered with (\g<operator> and \g<expression>)* | |
( | |
\s* | |
\g<operator> | |
\s* | |
\g<expression> | |
\s* # Probably I don't need this. But I wrote it like this there, so... | |
)* | |
){0} | |
# This will surround any expression with brackets | |
(?<expression_with_brackets> | |
\g<sign> | |
\( | |
# Inside the brackets: | |
\s* | |
\g<expression> | |
\s* | |
\) | |
){0} | |
(?<expression> | |
\g<basic_expression> | |
| | |
\g<expression_with_brackets> | |
# Don't need the \g<val> as I initialy thought half a day ago; | |
# basic_expression already covers that case: uses \g<val> and | |
# no operator with another expression, i.e. - \g<val>(\g<opeator>\g<expression>){0}. | |
){0} | |
# The main expression: | |
\A | |
\s* | |
\g<expression> # This will start the whole recursive madness! | |
\s* | |
\z | |
}x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is an improved version of the one in the class. | |
# Includes longer variables and floats and complex numbers. | |
# Note: Also untested!!! | |
%r{ | |
# Named Groups declarations | |
(?<sign> | |
[-+]? | |
){0} | |
(?<int> | |
\d+ | |
(_\d*)* | |
){0} | |
(?<float> | |
\g<int> # Initially it was \g<int>*, but the irb complained when I entered ".1", but not when "0.1" | |
\. | |
\g<int> | |
){0} | |
(?<complex> | |
\g<float> | |
[i] | |
){0} | |
(?<normal_var_name> | |
( | |
(@|@@|\$)? # @ - local variable, @@ - class variable, $ - global, otherwise - it is local. | |
[_a-zA-Z] # Variable can only start with _ and a letter. | |
\w* # Numbers can follow only afterwards. | |
) | |
){0} | |
(?<operator> | |
[-+*\/] | |
){0} | |
(?<val> | |
( | |
\g<sign>\g<int> | |
| | |
\g<sign>\g<float> | |
| | |
\g<sign>\g<complex> | |
| | |
\g<sign>\g<normal_var_name> | |
) | |
){0} | |
(?<basic_expression> | |
( | |
\g<val> | |
| | |
\g<expression_with_brackets> | |
) # Use only val and expression_with_brackets and not just \g<expression>. | |
# Otherwise: | |
# 1) infinite recursion; | |
# 2) All other cases should be covered with (\g<operator> and \g<expression>)* | |
( | |
\s* | |
\g<operator> | |
\s* | |
\g<expression> | |
\s* # Probably I don't need this. But I wrote it like this there, so... | |
)* | |
){0} | |
# This will surround any expression with brackets | |
(?<expression_with_brackets> | |
\g<sign> | |
\( | |
# Inside the brackets: | |
\s* | |
\g<expression> | |
\s* | |
\) | |
){0} | |
(?<expression> | |
\g<basic_expression> | |
| | |
\g<expression_with_brackets> | |
# Don't need the \g<val> as I initialy thought half a day ago; | |
# basic_expression already covers that case: uses \g<val> and | |
# no operator with another expression, i.e. - \g<val>(\g<opeator>\g<expression>){0}. | |
){0} | |
# The main expression: | |
\A | |
\s* | |
\g<expression> # This will start the whole recursive madness! | |
\s* | |
\z | |
}x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution I did at home | |
module MathExpressionValidator | |
require './regex_extension' | |
# Prototypes | |
class RegexPrototypes | |
INTEGER_REGEX = /(\-|\+)?(\d)+(_(\d)+)*/ | |
INTEGER_REGEX_WITH_GROUPS = /(?<integer>(\-|\+)?(\d)+(_(\d)+)*)/ | |
VARIABLE_REGEX = /(\-|\+)?[a-z]/ | |
VARIABLE_REGEX_WITH_GROUPS = /(?<variable>(\-|\+)?[a-z])/ | |
INTEGER_OR_VARIABLE = /((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])/ | |
INTEGER_OR_VARIABLE_WITH_GROUPS = | |
/(?<int_or_var>(\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])/ | |
OPERATOR_REGEX = /(\+|\-|\*|\/)/ | |
OPERATOR_REGEX_WITH_GROUPS = /(?<operator>\+|\-|\*|\/)/ | |
SIMPLE_EXPRESSION_REGEX = | |
/((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])(\s*(\+|\-|\*|\/)\s*((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z]))*/ | |
SIMPLE_EXPRESSION_SURROUNDED_BY_BRACKETS_REGEX = | |
/\(\s*/ + SIMPLE_EXPRESSION_REGEX + /\s*\)/ | |
SIMPLE_EXPRESSION_WITH_GROUPS_REGEX = | |
/(?<int_or_var>(\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])(\s*(?<operator>\+|\-|\*|\/)\s*\g<int_or_var>)*/ | |
COMPLEX_EXPRESSION_REGEX = %r{ | |
# expression parts | |
(?<sign> | |
(\-|\+)? | |
){0} | |
(?<int> | |
( | |
\d+ | |
(_(\d)+)* | |
) | |
){0} | |
(?<var> | |
[a-z] | |
){0} | |
(?<int_or_var> | |
( | |
\g<sign>\g<int> | |
| | |
\g<sign>\g<var> | |
) | |
){0} | |
(?<operator> | |
( | |
\+| | |
\-| | |
\*| | |
\/ | |
) | |
){0} | |
(?<short_expression> | |
( | |
( | |
\g<int_or_var> | |
| | |
\g<expression_in_brackets> | |
) | |
( | |
\s* | |
\g<operator> | |
\s* | |
\g<expression> | |
)+ | |
) | |
){0} | |
(?<expression_in_brackets> | |
( | |
\g<sign> | |
\( | |
\s* | |
\g<expression> | |
\s* | |
\) | |
) | |
){0} | |
(?<expression> | |
( | |
\g<int_or_var> | |
| | |
\g<expression_in_brackets> | |
| | |
\g<short_expression> | |
) | |
){0} | |
# The expression itself | |
\A | |
\s* | |
\g<expression> | |
\s* | |
\z | |
}x | |
class << self | |
def get_integer_regex_only | |
INTEGER_REGEX.convert_for_exact_match | |
end | |
def get_variable_regex_only | |
VARIABLE_REGEX.convert_for_exact_match | |
end | |
def get_operator_regex_only | |
OPERATOR_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_regex_only | |
SIMPLE_EXPRESSION_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_surrounded_by_brackets_regex_only | |
SIMPLE_EXPRESSION_SURROUNDED_BY_BRACKETS_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_with_groups_regex_only | |
SIMPLE_EXPRESSION_WITH_GROUPS_REGEX.convert_for_exact_match | |
end | |
def get_complex_expression_regex_only | |
COMPLEX_EXPRESSION_REGEX.convert_for_exact_match | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution I did at home (fixed) | |
# With this I tried to stick close to the description as much as possible, | |
# i.e. no fancy checking, other types as floats and complex numbers, | |
# longer variables (not just the one letter one)... | |
# For extras, look in the last file (math_expression_class_extended.rb). | |
# The fix: removed ".convert_for_exact_match" from COMPLEX_EXPRESSION_REGEX | |
# This way the result won't be ^\A....\z$ , which is stupid, since | |
# \A and \z match the begining and end of the string, | |
# but ^ and $ will match only one line (until '\n'). | |
# In short: it would probably break when it encounters end of line. | |
module MathExpressionValidator | |
require './regex_extension' | |
# Prototypes | |
class RegexPrototypes | |
INTEGER_REGEX = /(\-|\+)?(\d)+(_(\d)+)*/ | |
INTEGER_REGEX_WITH_GROUPS = /(?<integer>(\-|\+)?(\d)+(_(\d)+)*)/ | |
VARIABLE_REGEX = /(\-|\+)?[a-z]/ | |
VARIABLE_REGEX_WITH_GROUPS = /(?<variable>(\-|\+)?[a-z])/ | |
INTEGER_OR_VARIABLE = /((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])/ | |
INTEGER_OR_VARIABLE_WITH_GROUPS = | |
/(?<int_or_var>(\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])/ | |
OPERATOR_REGEX = /(\+|\-|\*|\/)/ | |
OPERATOR_REGEX_WITH_GROUPS = /(?<operator>\+|\-|\*|\/)/ | |
SIMPLE_EXPRESSION_REGEX = | |
/((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])(\s*(\+|\-|\*|\/)\s*((\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z]))*/ | |
SIMPLE_EXPRESSION_SURROUNDED_BY_BRACKETS_REGEX = | |
/\(\s*/ + SIMPLE_EXPRESSION_REGEX + /\s*\)/ | |
SIMPLE_EXPRESSION_WITH_GROUPS_REGEX = | |
/(?<int_or_var>(\-|\+)?(\d)+(_(\d)+)*|(\-|\+)?[a-z])(\s*(?<operator>\+|\-|\*|\/)\s*\g<int_or_var>)*/ | |
COMPLEX_EXPRESSION_REGEX = %r{ | |
# expression parts | |
(?<sign> | |
(\-|\+)? | |
){0} | |
(?<int> | |
( | |
\d+ | |
(_(\d)+)* | |
) | |
){0} | |
(?<var> | |
[a-z] | |
){0} | |
(?<int_or_var> | |
( | |
\g<sign>\g<int> | |
| | |
\g<sign>\g<var> | |
) | |
){0} | |
(?<operator> | |
( | |
\+| | |
\-| | |
\*| | |
\/ | |
) | |
){0} | |
(?<short_expression> | |
( | |
( | |
\g<int_or_var> | |
| | |
\g<expression_in_brackets> | |
) | |
( | |
\s* | |
\g<operator> | |
\s* | |
\g<expression> | |
)+ | |
) | |
){0} | |
(?<expression_in_brackets> | |
( | |
\g<sign> | |
\( | |
\s* | |
\g<expression> | |
\s* | |
\) | |
) | |
){0} | |
(?<expression> | |
( | |
\g<int_or_var> | |
| | |
\g<expression_in_brackets> | |
| | |
\g<short_expression> | |
) | |
){0} | |
# The expression itself | |
\A | |
\s* | |
\g<expression> | |
\s* | |
\z | |
}x | |
class << self | |
def get_integer_regex_only | |
INTEGER_REGEX.convert_for_exact_match | |
end | |
def get_variable_regex_only | |
VARIABLE_REGEX.convert_for_exact_match | |
end | |
def get_operator_regex_only | |
OPERATOR_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_regex_only | |
SIMPLE_EXPRESSION_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_surrounded_by_brackets_regex_only | |
SIMPLE_EXPRESSION_SURROUNDED_BY_BRACKETS_REGEX.convert_for_exact_match | |
end | |
def get_simple_expression_with_groups_regex_only | |
SIMPLE_EXPRESSION_WITH_GROUPS_REGEX.convert_for_exact_match | |
end | |
def get_complex_expression_regex_only | |
COMPLEX_EXPRESSION_REGEX | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The tests I used. Only the last 200 lines or so are about the complete version; | |
# the rest test the prototypes. | |
# Note: the tests are not that extensive, especially in the end, | |
# since it was already early morning. | |
require './regex_extension' | |
describe "MathExpressionValidator::RegexPrototypes" do | |
describe "INTEGER_REGEX" do | |
def integer_match string_integer | |
integer_regex = | |
MathExpressionValidator::RegexPrototypes::get_integer_regex_only | |
result = integer_regex.match(string_integer) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid integers" do | |
end | |
it "invalid integers" do | |
expect(integer_match("")).to eq(nil) | |
expect(integer_match("-")).to eq(nil) | |
expect(integer_match("+")).to eq(nil) | |
expect(integer_match("--")).to eq(nil) | |
expect(integer_match("-+")).to eq(nil) | |
expect(integer_match("+-")).to eq(nil) | |
expect(integer_match("++")).to eq(nil) | |
expect(integer_match("--123")).to eq(nil) | |
expect(integer_match("-+123")).to eq(nil) | |
expect(integer_match("+-123")).to eq(nil) | |
expect(integer_match("++123")).to eq(nil) | |
expect(integer_match("--1")).to eq(nil) | |
expect(integer_match("-+1")).to eq(nil) | |
expect(integer_match("+-1")).to eq(nil) | |
expect(integer_match("++1")).to eq(nil) | |
expect(integer_match("-123-")).to eq(nil) | |
expect(integer_match("-123+")).to eq(nil) | |
expect(integer_match("+123-")).to eq(nil) | |
expect(integer_match("+123+")).to eq(nil) | |
expect(integer_match("-1-")).to eq(nil) | |
expect(integer_match("-1+")).to eq(nil) | |
expect(integer_match("+1-")).to eq(nil) | |
expect(integer_match("+1+")).to eq(nil) | |
expect(integer_match("a")).to eq(nil) | |
expect(integer_match("-a")).to eq(nil) | |
expect(integer_match("+a")).to eq(nil) | |
expect(integer_match("asdf")).to eq(nil) | |
expect(integer_match("-asdf")).to eq(nil) | |
expect(integer_match("+asdf")).to eq(nil) | |
end | |
it "valid integers with underscores" do | |
expect(integer_match("12_3")).to eq("12_3") | |
expect(integer_match("-12_3")).to eq("-12_3") | |
expect(integer_match("+12_3")).to eq("+12_3") | |
expect(integer_match("12_300_12")).to eq("12_300_12") | |
expect(integer_match("-12_300_12")).to eq("-12_300_12") | |
expect(integer_match("+12_300_12")).to eq("+12_300_12") | |
expect(integer_match("12_300_000")).to eq("12_300_000") | |
expect(integer_match("-12_300_000")).to eq("-12_300_000") | |
expect(integer_match("+12_300_000")).to eq("+12_300_000") | |
expect(integer_match("0_00")).to eq("0_00") | |
expect(integer_match("-0_00")).to eq("-0_00") | |
expect(integer_match("+0_00")).to eq("+0_00") | |
end | |
it "invalid integers with underscores" do | |
expect(integer_match("as_df")).to eq(nil) | |
expect(integer_match("-as_df")).to eq(nil) | |
expect(integer_match("+as_df")).to eq(nil) | |
expect(integer_match("asdf_")).to eq(nil) | |
expect(integer_match("-as_df_")).to eq(nil) | |
expect(integer_match("+asdf_")).to eq(nil) | |
expect(integer_match("_asdf")).to eq(nil) | |
expect(integer_match("-_asdf")).to eq(nil) | |
expect(integer_match("+_asdf")).to eq(nil) | |
expect(integer_match("_1")).to eq(nil) | |
expect(integer_match("-_1")).to eq(nil) | |
expect(integer_match("+_1")).to eq(nil) | |
expect(integer_match("1_")).to eq(nil) | |
expect(integer_match("-1_")).to eq(nil) | |
expect(integer_match("+1_")).to eq(nil) | |
expect(integer_match("_100_11")).to eq(nil) | |
expect(integer_match("-_100_11")).to eq(nil) | |
expect(integer_match("+_100_11")).to eq(nil) | |
expect(integer_match("_100_11_")).to eq(nil) | |
expect(integer_match("-_100_11_")).to eq(nil) | |
expect(integer_match("+_100_11_")).to eq(nil) | |
expect(integer_match("100_11_")).to eq(nil) | |
expect(integer_match("-100_11_")).to eq(nil) | |
expect(integer_match("+100_11_")).to eq(nil) | |
end | |
end | |
describe "VARIABLE_REGEX" do | |
def variable_match string_variable | |
variable_regex = | |
MathExpressionValidator::RegexPrototypes.get_variable_regex_only | |
result = variable_regex.match(string_variable) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid variable" do | |
expect(variable_match("a")).to eq("a") | |
expect(variable_match("-a")).to eq("-a") | |
expect(variable_match("+a")).to eq("+a") | |
expect(variable_match("b")).to eq("b") | |
expect(variable_match("-b")).to eq("-b") | |
expect(variable_match("+b")).to eq("+b") | |
expect(variable_match("c")).to eq("c") | |
expect(variable_match("-c")).to eq("-c") | |
expect(variable_match("+c")).to eq("+c") | |
expect(variable_match("i")).to eq("i") | |
expect(variable_match("-i")).to eq("-i") | |
expect(variable_match("+i")).to eq("+i") | |
expect(variable_match("x")).to eq("x") | |
expect(variable_match("-x")).to eq("-x") | |
expect(variable_match("+x")).to eq("+x") | |
expect(variable_match("y")).to eq("y") | |
expect(variable_match("-y")).to eq("-y") | |
expect(variable_match("+y")).to eq("+y") | |
expect(variable_match("z")).to eq("z") | |
expect(variable_match("-z")).to eq("-z") | |
expect(variable_match("+z")).to eq("+z") | |
end | |
it "invalid integers" do | |
expect(variable_match("")).to eq(nil) | |
expect(variable_match("-")).to eq(nil) | |
expect(variable_match("+")).to eq(nil) | |
expect(variable_match("--")).to eq(nil) | |
expect(variable_match("-+")).to eq(nil) | |
expect(variable_match("+-")).to eq(nil) | |
expect(variable_match("++")).to eq(nil) | |
expect(variable_match("--123")).to eq(nil) | |
expect(variable_match("-+123")).to eq(nil) | |
expect(variable_match("+-123")).to eq(nil) | |
expect(variable_match("++123")).to eq(nil) | |
expect(variable_match("--1")).to eq(nil) | |
expect(variable_match("-+1")).to eq(nil) | |
expect(variable_match("+-1")).to eq(nil) | |
expect(variable_match("++1")).to eq(nil) | |
expect(variable_match("-123-")).to eq(nil) | |
expect(variable_match("-123+")).to eq(nil) | |
expect(variable_match("+123-")).to eq(nil) | |
expect(variable_match("+123+")).to eq(nil) | |
expect(variable_match("-1-")).to eq(nil) | |
expect(variable_match("-1+")).to eq(nil) | |
expect(variable_match("+1-")).to eq(nil) | |
expect(variable_match("+1+")).to eq(nil) | |
expect(variable_match("aa")).to eq(nil) | |
expect(variable_match("-aa")).to eq(nil) | |
expect(variable_match("+aa")).to eq(nil) | |
expect(variable_match("a_a")).to eq(nil) | |
expect(variable_match("-a_a")).to eq(nil) | |
expect(variable_match("+a_a")).to eq(nil) | |
expect(variable_match("_")).to eq(nil) | |
expect(variable_match("-_")).to eq(nil) | |
expect(variable_match("+_")).to eq(nil) | |
expect(variable_match("A")).to eq(nil) | |
expect(variable_match("-A")).to eq(nil) | |
expect(variable_match("+A")).to eq(nil) | |
expect(variable_match("AA")).to eq(nil) | |
expect(variable_match("-AA")).to eq(nil) | |
expect(variable_match("+AA")).to eq(nil) | |
expect(variable_match("asdf")).to eq(nil) | |
expect(variable_match("-asdf")).to eq(nil) | |
expect(variable_match("+asdf")).to eq(nil) | |
expect(variable_match("123")).to eq(nil) | |
expect(variable_match("-123")).to eq(nil) | |
expect(variable_match("+123")).to eq(nil) | |
expect(variable_match("1")).to eq(nil) | |
expect(variable_match("-1")).to eq(nil) | |
expect(variable_match("+1")).to eq(nil) | |
expect(variable_match("0")).to eq(nil) | |
expect(variable_match("-0")).to eq(nil) | |
expect(variable_match("+0")).to eq(nil) | |
expect(variable_match("000")).to eq(nil) | |
expect(variable_match("-000")).to eq(nil) | |
expect(variable_match("+000")).to eq(nil) | |
end | |
end | |
describe "OPERATOR_REGEX" do | |
def operator_match string_operator | |
operator_regex = | |
MathExpressionValidator::RegexPrototypes.get_operator_regex_only | |
result = operator_regex.match(string_operator) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid operator" do | |
expect(operator_match("+")).to eq("+") | |
expect(operator_match("-")).to eq("-") | |
expect(operator_match("*")).to eq("*") | |
expect(operator_match("/")).to eq("/") | |
end | |
it "invalid operator" do | |
expect(operator_match("")).to eq(nil) | |
expect(operator_match("--")).to eq(nil) | |
expect(operator_match("-+")).to eq(nil) | |
expect(operator_match("-*")).to eq(nil) | |
expect(operator_match("-/")).to eq(nil) | |
expect(operator_match("+-")).to eq(nil) | |
expect(operator_match("++")).to eq(nil) | |
expect(operator_match("+*")).to eq(nil) | |
expect(operator_match("+/")).to eq(nil) | |
expect(operator_match("*-")).to eq(nil) | |
expect(operator_match("*+")).to eq(nil) | |
expect(operator_match("**")).to eq(nil) | |
expect(operator_match("*/")).to eq(nil) | |
expect(operator_match("/-")).to eq(nil) | |
expect(operator_match("/+")).to eq(nil) | |
expect(operator_match("/*")).to eq(nil) | |
expect(operator_match("//")).to eq(nil) | |
expect(operator_match("_-")).to eq(nil) | |
expect(operator_match("_+")).to eq(nil) | |
expect(operator_match("_*")).to eq(nil) | |
expect(operator_match("_/")).to eq(nil) | |
expect(operator_match("-_")).to eq(nil) | |
expect(operator_match("+_")).to eq(nil) | |
expect(operator_match("*_")).to eq(nil) | |
expect(operator_match("/_")).to eq(nil) | |
expect(operator_match("1-")).to eq(nil) | |
expect(operator_match("1+")).to eq(nil) | |
expect(operator_match("1*")).to eq(nil) | |
expect(operator_match("1/")).to eq(nil) | |
expect(operator_match("-1")).to eq(nil) | |
expect(operator_match("+1")).to eq(nil) | |
expect(operator_match("*1")).to eq(nil) | |
expect(operator_match("/1")).to eq(nil) | |
expect(operator_match("a-")).to eq(nil) | |
expect(operator_match("a+")).to eq(nil) | |
expect(operator_match("a*")).to eq(nil) | |
expect(operator_match("a/")).to eq(nil) | |
expect(operator_match("-a")).to eq(nil) | |
expect(operator_match("+a")).to eq(nil) | |
expect(operator_match("*a")).to eq(nil) | |
expect(operator_match("/a")).to eq(nil) | |
end | |
end | |
describe "SIMPLE_EXPRESSION_REGEX" do | |
def simple_expression_match string_simple_expression | |
simple_expression_regex = | |
MathExpressionValidator::RegexPrototypes.get_simple_expression_regex_only | |
result = simple_expression_regex.match(string_simple_expression) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid simple expression" do | |
expect(simple_expression_match("1")).to eq("1") | |
expect(simple_expression_match("-1")).to eq("-1") | |
expect(simple_expression_match("+1")).to eq("+1") | |
expect(simple_expression_match("x")).to eq("x") | |
expect(simple_expression_match("-x")).to eq("-x") | |
expect(simple_expression_match("+x")).to eq("+x") | |
expect(simple_expression_match("1 + 2")).to eq("1 + 2") | |
expect(simple_expression_match("1 + x")).to eq("1 + x") | |
expect(simple_expression_match("x + 2")).to eq("x + 2") | |
expect(simple_expression_match("x + x")).to eq("x + x") | |
expect(simple_expression_match("x + a")).to eq("x + a") | |
expect(simple_expression_match("-1 + 2")).to eq("-1 + 2") | |
expect(simple_expression_match("-1 + x")).to eq("-1 + x") | |
expect(simple_expression_match("-x + 2")).to eq("-x + 2") | |
expect(simple_expression_match("-x + x")).to eq("-x + x") | |
expect(simple_expression_match("-x + a")).to eq("-x + a") | |
expect(simple_expression_match("1 + -2")).to eq("1 + -2") | |
expect(simple_expression_match("1 + -x")).to eq("1 + -x") | |
expect(simple_expression_match("x + -2")).to eq("x + -2") | |
expect(simple_expression_match("x + -x")).to eq("x + -x") | |
expect(simple_expression_match("x + -a")).to eq("x + -a") | |
expect(simple_expression_match("-1 + -2")).to eq("-1 + -2") | |
expect(simple_expression_match("-1 + -x")).to eq("-1 + -x") | |
expect(simple_expression_match("-x + -2")).to eq("-x + -2") | |
expect(simple_expression_match("-x + -x")).to eq("-x + -x") | |
expect(simple_expression_match("-x + -a")).to eq("-x + -a") | |
expect(simple_expression_match("+1 + 2")).to eq("+1 + 2") | |
expect(simple_expression_match("+1 + x")).to eq("+1 + x") | |
expect(simple_expression_match("+x + 2")).to eq("+x + 2") | |
expect(simple_expression_match("+x + x")).to eq("+x + x") | |
expect(simple_expression_match("+x + a")).to eq("+x + a") | |
expect(simple_expression_match("1 + +2")).to eq("1 + +2") | |
expect(simple_expression_match("1 + +x")).to eq("1 + +x") | |
expect(simple_expression_match("x + +2")).to eq("x + +2") | |
expect(simple_expression_match("x + +x")).to eq("x + +x") | |
expect(simple_expression_match("x + +a")).to eq("x + +a") | |
expect(simple_expression_match("+1 + +2")).to eq("+1 + +2") | |
expect(simple_expression_match("+1 + +x")).to eq("+1 + +x") | |
expect(simple_expression_match("+x + +2")).to eq("+x + +2") | |
expect(simple_expression_match("+x + +x")).to eq("+x + +x") | |
expect(simple_expression_match("+x + +a")).to eq("+x + +a") | |
expect(simple_expression_match("-1 + +2")).to eq("-1 + +2") | |
expect(simple_expression_match("-1 + +x")).to eq("-1 + +x") | |
expect(simple_expression_match("-x + +2")).to eq("-x + +2") | |
expect(simple_expression_match("-x + +x")).to eq("-x + +x") | |
expect(simple_expression_match("-x + +a")).to eq("-x + +a") | |
expect(simple_expression_match("+1 + -2")).to eq("+1 + -2") | |
expect(simple_expression_match("+1 + -x")).to eq("+1 + -x") | |
expect(simple_expression_match("+x + -2")).to eq("+x + -2") | |
expect(simple_expression_match("+x + -x")).to eq("+x + -x") | |
expect(simple_expression_match("+x + -a")).to eq("+x + -a") | |
expect(simple_expression_match("1+2")).to eq("1+2") | |
expect(simple_expression_match("1+x")).to eq("1+x") | |
expect(simple_expression_match("x+2")).to eq("x+2") | |
expect(simple_expression_match("x+x")).to eq("x+x") | |
expect(simple_expression_match("x+a")).to eq("x+a") | |
expect(simple_expression_match("1+-2")).to eq("1+-2") | |
expect(simple_expression_match("1+-x")).to eq("1+-x") | |
expect(simple_expression_match("x+-2")).to eq("x+-2") | |
expect(simple_expression_match("x+-x")).to eq("x+-x") | |
expect(simple_expression_match("x+-a")).to eq("x+-a") | |
expect(simple_expression_match("1++2")).to eq("1++2") | |
expect(simple_expression_match("1++x")).to eq("1++x") | |
expect(simple_expression_match("x++2")).to eq("x++2") | |
expect(simple_expression_match("x++x")).to eq("x++x") | |
expect(simple_expression_match("x++a")).to eq("x++a") | |
expect(simple_expression_match("-1+2")).to eq("-1+2") | |
expect(simple_expression_match("-1+x")).to eq("-1+x") | |
expect(simple_expression_match("-x+2")).to eq("-x+2") | |
expect(simple_expression_match("-x+x")).to eq("-x+x") | |
expect(simple_expression_match("-x+a")).to eq("-x+a") | |
expect(simple_expression_match("+1+2")).to eq("+1+2") | |
expect(simple_expression_match("+1+x")).to eq("+1+x") | |
expect(simple_expression_match("+x+2")).to eq("+x+2") | |
expect(simple_expression_match("+x+x")).to eq("+x+x") | |
expect(simple_expression_match("+x+a")).to eq("+x+a") | |
expect(simple_expression_match("+1+-2")).to eq("+1+-2") | |
expect(simple_expression_match("+1+-x")).to eq("+1+-x") | |
expect(simple_expression_match("+x+-2")).to eq("+x+-2") | |
expect(simple_expression_match("+x+-x")).to eq("+x+-x") | |
expect(simple_expression_match("+x+-a")).to eq("+x+-a") | |
expect(simple_expression_match("-1+-2")).to eq("-1+-2") | |
expect(simple_expression_match("-1+-x")).to eq("-1+-x") | |
expect(simple_expression_match("-x+-2")).to eq("-x+-2") | |
expect(simple_expression_match("-x+-x")).to eq("-x+-x") | |
expect(simple_expression_match("-x+-a")).to eq("-x+-a") | |
expect(simple_expression_match("+1++2")).to eq("+1++2") | |
expect(simple_expression_match("+1++x")).to eq("+1++x") | |
expect(simple_expression_match("+x++2")).to eq("+x++2") | |
expect(simple_expression_match("+x++x")).to eq("+x++x") | |
expect(simple_expression_match("+x++a")).to eq("+x++a") | |
expect(simple_expression_match("1-2")).to eq("1-2") | |
expect(simple_expression_match("1-x")).to eq("1-x") | |
expect(simple_expression_match("x-2")).to eq("x-2") | |
expect(simple_expression_match("x-x")).to eq("x-x") | |
expect(simple_expression_match("x-a")).to eq("x-a") | |
expect(simple_expression_match("1*2")).to eq("1*2") | |
expect(simple_expression_match("1*x")).to eq("1*x") | |
expect(simple_expression_match("x*2")).to eq("x*2") | |
expect(simple_expression_match("x*x")).to eq("x*x") | |
expect(simple_expression_match("x*a")).to eq("x*a") | |
expect(simple_expression_match("1/2")).to eq("1/2") | |
expect(simple_expression_match("1/x")).to eq("1/x") | |
expect(simple_expression_match("x/2")).to eq("x/2") | |
expect(simple_expression_match("x/x")).to eq("x/x") | |
expect(simple_expression_match("x/a")).to eq("x/a") | |
end | |
it "invalid simple expression" do | |
expect(simple_expression_match("")).to eq(nil) | |
expect(simple_expression_match("1+++2")).to eq(nil) | |
expect(simple_expression_match("1+++x")).to eq(nil) | |
expect(simple_expression_match("x+++2")).to eq(nil) | |
expect(simple_expression_match("x+++x")).to eq(nil) | |
expect(simple_expression_match("x+++a")).to eq(nil) | |
expect(simple_expression_match("1---2")).to eq(nil) | |
expect(simple_expression_match("1---x")).to eq(nil) | |
expect(simple_expression_match("x---2")).to eq(nil) | |
expect(simple_expression_match("x---x")).to eq(nil) | |
expect(simple_expression_match("x---a")).to eq(nil) | |
expect(simple_expression_match("1-+-2")).to eq(nil) | |
expect(simple_expression_match("1-+-x")).to eq(nil) | |
expect(simple_expression_match("x-+-2")).to eq(nil) | |
expect(simple_expression_match("x-+-x")).to eq(nil) | |
expect(simple_expression_match("x-+-a")).to eq(nil) | |
expect(simple_expression_match("1+*2")).to eq(nil) | |
expect(simple_expression_match("1+/x")).to eq(nil) | |
expect(simple_expression_match("x-*2")).to eq(nil) | |
expect(simple_expression_match("x-/x")).to eq(nil) | |
expect(simple_expression_match("x*/a")).to eq(nil) | |
expect(simple_expression_match("x/*a")).to eq(nil) | |
end | |
it "valid multiple simple expressions" do | |
expect(simple_expression_match("1 + 2 + 3")).to eq("1 + 2 + 3") | |
expect(simple_expression_match("x + 2 + 3")).to eq("x + 2 + 3") | |
expect(simple_expression_match("1 + x + 3")).to eq("1 + x + 3") | |
expect(simple_expression_match("1 + 2 + x")).to eq("1 + 2 + x") | |
expect(simple_expression_match("x + y + 3")).to eq("x + y + 3") | |
expect(simple_expression_match("1 + x + y")).to eq("1 + x + y") | |
expect(simple_expression_match("y + 2 + x")).to eq("y + 2 + x") | |
expect(simple_expression_match("x + 2 + y")).to eq("x + 2 + y") | |
expect(simple_expression_match("y + x + 3")).to eq("y + x + 3") | |
expect(simple_expression_match("1 + y + x")).to eq("1 + y + x") | |
expect(simple_expression_match("x + z + y")).to eq("x + z + y") | |
expect(simple_expression_match("y + x + z")).to eq("y + x + z") | |
expect(simple_expression_match("z + y + x")).to eq("z + y + x") | |
expect(simple_expression_match("1 - 2 - 3")).to eq("1 - 2 - 3") | |
expect(simple_expression_match("x - 2 + 3")).to eq("x - 2 + 3") | |
expect(simple_expression_match("1 + x - 3")).to eq("1 + x - 3") | |
expect(simple_expression_match("1 / 2 * x")).to eq("1 / 2 * x") | |
expect(simple_expression_match("-1 - 2 - 3")).to eq("-1 - 2 - 3") | |
expect(simple_expression_match("-x - 2 + 3")).to eq("-x - 2 + 3") | |
expect(simple_expression_match("-1 + x - 3")).to eq("-1 + x - 3") | |
expect(simple_expression_match("-1 / 2 * x")).to eq("-1 / 2 * x") | |
expect(simple_expression_match("+1 - 2 - 3")).to eq("+1 - 2 - 3") | |
expect(simple_expression_match("+x - 2 + 3")).to eq("+x - 2 + 3") | |
expect(simple_expression_match("+1 + x - 3")).to eq("+1 + x - 3") | |
expect(simple_expression_match("+1 / 2 * x")).to eq("+1 / 2 * x") | |
expect(simple_expression_match("1 -2 - 3")).to eq("1 -2 - 3") | |
expect(simple_expression_match("x -2 + 3")).to eq("x -2 + 3") | |
expect(simple_expression_match("1 +x - 3")).to eq("1 +x - 3") | |
expect(simple_expression_match("1 /2 * x")).to eq("1 /2 * x") | |
expect(simple_expression_match("-1 -2 - 3")).to eq("-1 -2 - 3") | |
expect(simple_expression_match("-x -2 + 3")).to eq("-x -2 + 3") | |
expect(simple_expression_match("-1 +x - 3")).to eq("-1 +x - 3") | |
expect(simple_expression_match("-1 /2 * x")).to eq("-1 /2 * x") | |
expect(simple_expression_match("+1 -2 - 3")).to eq("+1 -2 - 3") | |
expect(simple_expression_match("+x -2 + 3")).to eq("+x -2 + 3") | |
expect(simple_expression_match("+1 +x - 3")).to eq("+1 +x - 3") | |
expect(simple_expression_match("+1 /2 * x")).to eq("+1 /2 * x") | |
expect(simple_expression_match("1 --2 - 3")).to eq("1 --2 - 3") | |
expect(simple_expression_match("x --2 + 3")).to eq("x --2 + 3") | |
expect(simple_expression_match("1 +-x - 3")).to eq("1 +-x - 3") | |
expect(simple_expression_match("1 /-2 * x")).to eq("1 /-2 * x") | |
expect(simple_expression_match("-1 --2 - 3")).to eq("-1 --2 - 3") | |
expect(simple_expression_match("-x --2 + 3")).to eq("-x --2 + 3") | |
expect(simple_expression_match("-1 +-x - 3")).to eq("-1 +-x - 3") | |
expect(simple_expression_match("-1 /-2 * x")).to eq("-1 /-2 * x") | |
expect(simple_expression_match("+1 --2 - 3")).to eq("+1 --2 - 3") | |
expect(simple_expression_match("+x --2 + 3")).to eq("+x --2 + 3") | |
expect(simple_expression_match("+1 +-x - 3")).to eq("+1 +-x - 3") | |
expect(simple_expression_match("+1 /-2 * x")).to eq("+1 /-2 * x") | |
end | |
end | |
describe "SIMPLE_EXPRESSION_SURROUNDED_BY_BRACKETS_REGEX" do | |
def simple_expression_with_brackets_match string_simple_expression_with_brackets | |
simple_expression_surrounded_by_brackets_regex = | |
MathExpressionValidator::RegexPrototypes.get_simple_expression_surrounded_by_brackets_regex_only | |
result = simple_expression_surrounded_by_brackets_regex.match(string_simple_expression_with_brackets) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid simple expression" do | |
expect(simple_expression_with_brackets_match("(1)")).to eq("(1)") | |
expect(simple_expression_with_brackets_match("(-1)")).to eq("(-1)") | |
expect(simple_expression_with_brackets_match("(+1)")).to eq("(+1)") | |
expect(simple_expression_with_brackets_match("(x)")).to eq("(x)") | |
expect(simple_expression_with_brackets_match("(-x)")).to eq("(-x)") | |
expect(simple_expression_with_brackets_match("(+x)")).to eq("(+x)") | |
expect(simple_expression_with_brackets_match("(1+1)")).to eq("(1+1)") | |
expect(simple_expression_with_brackets_match("(x+1)")).to eq("(x+1)") | |
expect(simple_expression_with_brackets_match("(-1+x)")).to eq("(-1+x)") | |
expect(simple_expression_with_brackets_match("(+1*y)")).to eq("(+1*y)") | |
end | |
it "invalid simple expression" do | |
expect(simple_expression_with_brackets_match("")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("-1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("+1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(x")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-x")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+x")).to eq(nil) | |
expect(simple_expression_with_brackets_match("x)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("-x)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("+x)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1+1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(x+1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-1+x")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+1*y")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1+1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("x+1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("-1+x)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("+1*y)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1+1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("x+1")).to eq(nil) | |
expect(simple_expression_with_brackets_match("-1+x")).to eq(nil) | |
expect(simple_expression_with_brackets_match("+1*y")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1+(1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(x+(1)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-1+(x)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+1*(y)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1+(1))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(x+(1))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-1+(x))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+1*(y))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1(+1))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(x(+1))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(-1(+x))")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(+1(*y))")).to eq(nil) | |
end | |
it "valid multiple simple expressions" do | |
expect(simple_expression_with_brackets_match("(1 + 2 + 3)")).to eq("(1 + 2 + 3)") | |
expect(simple_expression_with_brackets_match("(1+ x +3)")).to eq("(1+ x +3)") | |
end | |
it "invalid multiple simple expressions" do | |
expect(simple_expression_with_brackets_match("(1 + 2 + 3")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1+ x +3")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1 + 2 + 3)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1+ x +3)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1 + 2( + 3")).to eq(nil) | |
expect(simple_expression_with_brackets_match("(1+ x( +3")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1 + 2 )+ 3)")).to eq(nil) | |
expect(simple_expression_with_brackets_match("1+ x +)3)")).to eq(nil) | |
end | |
end | |
describe "SIMPLE_EXPRESSION_WITH_GROUPS_REGEX" do | |
def simple_expression_with_groups_match string_simple_expression_with_groups | |
simple_expression_with_groups_regex = | |
MathExpressionValidator::RegexPrototypes.get_simple_expression_with_groups_regex_only | |
result = simple_expression_with_groups_regex.match(string_simple_expression_with_groups) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid simple expression with groups" do | |
expect(simple_expression_with_groups_match("1")).to eq("1") | |
expect(simple_expression_with_groups_match("-1")).to eq("-1") | |
expect(simple_expression_with_groups_match("+1")).to eq("+1") | |
expect(simple_expression_with_groups_match("x")).to eq("x") | |
expect(simple_expression_with_groups_match("-x")).to eq("-x") | |
expect(simple_expression_with_groups_match("+x")).to eq("+x") | |
expect(simple_expression_with_groups_match("1 + 2")).to eq("1 + 2") | |
expect(simple_expression_with_groups_match("1 + x")).to eq("1 + x") | |
expect(simple_expression_with_groups_match("x + 2")).to eq("x + 2") | |
expect(simple_expression_with_groups_match("x + x")).to eq("x + x") | |
expect(simple_expression_with_groups_match("x + a")).to eq("x + a") | |
expect(simple_expression_with_groups_match("-1 + 2")).to eq("-1 + 2") | |
expect(simple_expression_with_groups_match("-1 + x")).to eq("-1 + x") | |
expect(simple_expression_with_groups_match("-x + 2")).to eq("-x + 2") | |
expect(simple_expression_with_groups_match("-x + x")).to eq("-x + x") | |
expect(simple_expression_with_groups_match("-x + a")).to eq("-x + a") | |
expect(simple_expression_with_groups_match("1 + -2")).to eq("1 + -2") | |
expect(simple_expression_with_groups_match("1 + -x")).to eq("1 + -x") | |
expect(simple_expression_with_groups_match("x + -2")).to eq("x + -2") | |
expect(simple_expression_with_groups_match("x + -x")).to eq("x + -x") | |
expect(simple_expression_with_groups_match("x + -a")).to eq("x + -a") | |
expect(simple_expression_with_groups_match("-1 + -2")).to eq("-1 + -2") | |
expect(simple_expression_with_groups_match("-1 + -x")).to eq("-1 + -x") | |
expect(simple_expression_with_groups_match("-x + -2")).to eq("-x + -2") | |
expect(simple_expression_with_groups_match("-x + -x")).to eq("-x + -x") | |
expect(simple_expression_with_groups_match("-x + -a")).to eq("-x + -a") | |
expect(simple_expression_with_groups_match("+1 + 2")).to eq("+1 + 2") | |
expect(simple_expression_with_groups_match("+1 + x")).to eq("+1 + x") | |
expect(simple_expression_with_groups_match("+x + 2")).to eq("+x + 2") | |
expect(simple_expression_with_groups_match("+x + x")).to eq("+x + x") | |
expect(simple_expression_with_groups_match("+x + a")).to eq("+x + a") | |
expect(simple_expression_with_groups_match("1 + +2")).to eq("1 + +2") | |
expect(simple_expression_with_groups_match("1 + +x")).to eq("1 + +x") | |
expect(simple_expression_with_groups_match("x + +2")).to eq("x + +2") | |
expect(simple_expression_with_groups_match("x + +x")).to eq("x + +x") | |
expect(simple_expression_with_groups_match("x + +a")).to eq("x + +a") | |
expect(simple_expression_with_groups_match("+1 + +2")).to eq("+1 + +2") | |
expect(simple_expression_with_groups_match("+1 + +x")).to eq("+1 + +x") | |
expect(simple_expression_with_groups_match("+x + +2")).to eq("+x + +2") | |
expect(simple_expression_with_groups_match("+x + +x")).to eq("+x + +x") | |
expect(simple_expression_with_groups_match("+x + +a")).to eq("+x + +a") | |
expect(simple_expression_with_groups_match("-1 + +2")).to eq("-1 + +2") | |
expect(simple_expression_with_groups_match("-1 + +x")).to eq("-1 + +x") | |
expect(simple_expression_with_groups_match("-x + +2")).to eq("-x + +2") | |
expect(simple_expression_with_groups_match("-x + +x")).to eq("-x + +x") | |
expect(simple_expression_with_groups_match("-x + +a")).to eq("-x + +a") | |
expect(simple_expression_with_groups_match("+1 + -2")).to eq("+1 + -2") | |
expect(simple_expression_with_groups_match("+1 + -x")).to eq("+1 + -x") | |
expect(simple_expression_with_groups_match("+x + -2")).to eq("+x + -2") | |
expect(simple_expression_with_groups_match("+x + -x")).to eq("+x + -x") | |
expect(simple_expression_with_groups_match("+x + -a")).to eq("+x + -a") | |
expect(simple_expression_with_groups_match("1+2")).to eq("1+2") | |
expect(simple_expression_with_groups_match("1+x")).to eq("1+x") | |
expect(simple_expression_with_groups_match("x+2")).to eq("x+2") | |
expect(simple_expression_with_groups_match("x+x")).to eq("x+x") | |
expect(simple_expression_with_groups_match("x+a")).to eq("x+a") | |
expect(simple_expression_with_groups_match("1+-2")).to eq("1+-2") | |
expect(simple_expression_with_groups_match("1+-x")).to eq("1+-x") | |
expect(simple_expression_with_groups_match("x+-2")).to eq("x+-2") | |
expect(simple_expression_with_groups_match("x+-x")).to eq("x+-x") | |
expect(simple_expression_with_groups_match("x+-a")).to eq("x+-a") | |
expect(simple_expression_with_groups_match("1++2")).to eq("1++2") | |
expect(simple_expression_with_groups_match("1++x")).to eq("1++x") | |
expect(simple_expression_with_groups_match("x++2")).to eq("x++2") | |
expect(simple_expression_with_groups_match("x++x")).to eq("x++x") | |
expect(simple_expression_with_groups_match("x++a")).to eq("x++a") | |
expect(simple_expression_with_groups_match("-1+2")).to eq("-1+2") | |
expect(simple_expression_with_groups_match("-1+x")).to eq("-1+x") | |
expect(simple_expression_with_groups_match("-x+2")).to eq("-x+2") | |
expect(simple_expression_with_groups_match("-x+x")).to eq("-x+x") | |
expect(simple_expression_with_groups_match("-x+a")).to eq("-x+a") | |
expect(simple_expression_with_groups_match("+1+2")).to eq("+1+2") | |
expect(simple_expression_with_groups_match("+1+x")).to eq("+1+x") | |
expect(simple_expression_with_groups_match("+x+2")).to eq("+x+2") | |
expect(simple_expression_with_groups_match("+x+x")).to eq("+x+x") | |
expect(simple_expression_with_groups_match("+x+a")).to eq("+x+a") | |
expect(simple_expression_with_groups_match("+1+-2")).to eq("+1+-2") | |
expect(simple_expression_with_groups_match("+1+-x")).to eq("+1+-x") | |
expect(simple_expression_with_groups_match("+x+-2")).to eq("+x+-2") | |
expect(simple_expression_with_groups_match("+x+-x")).to eq("+x+-x") | |
expect(simple_expression_with_groups_match("+x+-a")).to eq("+x+-a") | |
expect(simple_expression_with_groups_match("-1+-2")).to eq("-1+-2") | |
expect(simple_expression_with_groups_match("-1+-x")).to eq("-1+-x") | |
expect(simple_expression_with_groups_match("-x+-2")).to eq("-x+-2") | |
expect(simple_expression_with_groups_match("-x+-x")).to eq("-x+-x") | |
expect(simple_expression_with_groups_match("-x+-a")).to eq("-x+-a") | |
expect(simple_expression_with_groups_match("+1++2")).to eq("+1++2") | |
expect(simple_expression_with_groups_match("+1++x")).to eq("+1++x") | |
expect(simple_expression_with_groups_match("+x++2")).to eq("+x++2") | |
expect(simple_expression_with_groups_match("+x++x")).to eq("+x++x") | |
expect(simple_expression_with_groups_match("+x++a")).to eq("+x++a") | |
expect(simple_expression_with_groups_match("1-2")).to eq("1-2") | |
expect(simple_expression_with_groups_match("1-x")).to eq("1-x") | |
expect(simple_expression_with_groups_match("x-2")).to eq("x-2") | |
expect(simple_expression_with_groups_match("x-x")).to eq("x-x") | |
expect(simple_expression_with_groups_match("x-a")).to eq("x-a") | |
expect(simple_expression_with_groups_match("1*2")).to eq("1*2") | |
expect(simple_expression_with_groups_match("1*x")).to eq("1*x") | |
expect(simple_expression_with_groups_match("x*2")).to eq("x*2") | |
expect(simple_expression_with_groups_match("x*x")).to eq("x*x") | |
expect(simple_expression_with_groups_match("x*a")).to eq("x*a") | |
expect(simple_expression_with_groups_match("1/2")).to eq("1/2") | |
expect(simple_expression_with_groups_match("1/x")).to eq("1/x") | |
expect(simple_expression_with_groups_match("x/2")).to eq("x/2") | |
expect(simple_expression_with_groups_match("x/x")).to eq("x/x") | |
expect(simple_expression_with_groups_match("x/a")).to eq("x/a") | |
end | |
it "invalid simple expression with groups" do | |
expect(simple_expression_with_groups_match("")).to eq(nil) | |
expect(simple_expression_with_groups_match("1+++2")).to eq(nil) | |
expect(simple_expression_with_groups_match("1+++x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x+++2")).to eq(nil) | |
expect(simple_expression_with_groups_match("x+++x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x+++a")).to eq(nil) | |
expect(simple_expression_with_groups_match("1---2")).to eq(nil) | |
expect(simple_expression_with_groups_match("1---x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x---2")).to eq(nil) | |
expect(simple_expression_with_groups_match("x---x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x---a")).to eq(nil) | |
expect(simple_expression_with_groups_match("1-+-2")).to eq(nil) | |
expect(simple_expression_with_groups_match("1-+-x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x-+-2")).to eq(nil) | |
expect(simple_expression_with_groups_match("x-+-x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x-+-a")).to eq(nil) | |
expect(simple_expression_with_groups_match("1+*2")).to eq(nil) | |
expect(simple_expression_with_groups_match("1+/x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x-*2")).to eq(nil) | |
expect(simple_expression_with_groups_match("x-/x")).to eq(nil) | |
expect(simple_expression_with_groups_match("x*/a")).to eq(nil) | |
expect(simple_expression_with_groups_match("x/*a")).to eq(nil) | |
end | |
it "valid multiple simple expressions with groups" do | |
expect(simple_expression_with_groups_match("1 + 2 + 3")).to eq("1 + 2 + 3") | |
expect(simple_expression_with_groups_match("x + 2 + 3")).to eq("x + 2 + 3") | |
expect(simple_expression_with_groups_match("1 + x + 3")).to eq("1 + x + 3") | |
expect(simple_expression_with_groups_match("1 + 2 + x")).to eq("1 + 2 + x") | |
expect(simple_expression_with_groups_match("x + y + 3")).to eq("x + y + 3") | |
expect(simple_expression_with_groups_match("1 + x + y")).to eq("1 + x + y") | |
expect(simple_expression_with_groups_match("y + 2 + x")).to eq("y + 2 + x") | |
expect(simple_expression_with_groups_match("x + 2 + y")).to eq("x + 2 + y") | |
expect(simple_expression_with_groups_match("y + x + 3")).to eq("y + x + 3") | |
expect(simple_expression_with_groups_match("1 + y + x")).to eq("1 + y + x") | |
expect(simple_expression_with_groups_match("x + z + y")).to eq("x + z + y") | |
expect(simple_expression_with_groups_match("y + x + z")).to eq("y + x + z") | |
expect(simple_expression_with_groups_match("z + y + x")).to eq("z + y + x") | |
expect(simple_expression_with_groups_match("1 - 2 - 3")).to eq("1 - 2 - 3") | |
expect(simple_expression_with_groups_match("x - 2 + 3")).to eq("x - 2 + 3") | |
expect(simple_expression_with_groups_match("1 + x - 3")).to eq("1 + x - 3") | |
expect(simple_expression_with_groups_match("1 / 2 * x")).to eq("1 / 2 * x") | |
expect(simple_expression_with_groups_match("-1 - 2 - 3")).to eq("-1 - 2 - 3") | |
expect(simple_expression_with_groups_match("-x - 2 + 3")).to eq("-x - 2 + 3") | |
expect(simple_expression_with_groups_match("-1 + x - 3")).to eq("-1 + x - 3") | |
expect(simple_expression_with_groups_match("-1 / 2 * x")).to eq("-1 / 2 * x") | |
expect(simple_expression_with_groups_match("+1 - 2 - 3")).to eq("+1 - 2 - 3") | |
expect(simple_expression_with_groups_match("+x - 2 + 3")).to eq("+x - 2 + 3") | |
expect(simple_expression_with_groups_match("+1 + x - 3")).to eq("+1 + x - 3") | |
expect(simple_expression_with_groups_match("+1 / 2 * x")).to eq("+1 / 2 * x") | |
expect(simple_expression_with_groups_match("1 -2 - 3")).to eq("1 -2 - 3") | |
expect(simple_expression_with_groups_match("x -2 + 3")).to eq("x -2 + 3") | |
expect(simple_expression_with_groups_match("1 +x - 3")).to eq("1 +x - 3") | |
expect(simple_expression_with_groups_match("1 /2 * x")).to eq("1 /2 * x") | |
expect(simple_expression_with_groups_match("-1 -2 - 3")).to eq("-1 -2 - 3") | |
expect(simple_expression_with_groups_match("-x -2 + 3")).to eq("-x -2 + 3") | |
expect(simple_expression_with_groups_match("-1 +x - 3")).to eq("-1 +x - 3") | |
expect(simple_expression_with_groups_match("-1 /2 * x")).to eq("-1 /2 * x") | |
expect(simple_expression_with_groups_match("+1 -2 - 3")).to eq("+1 -2 - 3") | |
expect(simple_expression_with_groups_match("+x -2 + 3")).to eq("+x -2 + 3") | |
expect(simple_expression_with_groups_match("+1 +x - 3")).to eq("+1 +x - 3") | |
expect(simple_expression_with_groups_match("+1 /2 * x")).to eq("+1 /2 * x") | |
expect(simple_expression_with_groups_match("1 --2 - 3")).to eq("1 --2 - 3") | |
expect(simple_expression_with_groups_match("x --2 + 3")).to eq("x --2 + 3") | |
expect(simple_expression_with_groups_match("1 +-x - 3")).to eq("1 +-x - 3") | |
expect(simple_expression_with_groups_match("1 /-2 * x")).to eq("1 /-2 * x") | |
expect(simple_expression_with_groups_match("-1 --2 - 3")).to eq("-1 --2 - 3") | |
expect(simple_expression_with_groups_match("-x --2 + 3")).to eq("-x --2 + 3") | |
expect(simple_expression_with_groups_match("-1 +-x - 3")).to eq("-1 +-x - 3") | |
expect(simple_expression_with_groups_match("-1 /-2 * x")).to eq("-1 /-2 * x") | |
expect(simple_expression_with_groups_match("+1 --2 - 3")).to eq("+1 --2 - 3") | |
expect(simple_expression_with_groups_match("+x --2 + 3")).to eq("+x --2 + 3") | |
expect(simple_expression_with_groups_match("+1 +-x - 3")).to eq("+1 +-x - 3") | |
expect(simple_expression_with_groups_match("+1 /-2 * x")).to eq("+1 /-2 * x") | |
end | |
end | |
# The Grand Finale | |
describe "COMPLEX_EXPRESSION_REGEX" do | |
def complex_expression_match string_complex_expression | |
complex_expression_regex = | |
MathExpressionValidator::RegexPrototypes.get_complex_expression_regex_only | |
result = complex_expression_regex.match(string_complex_expression) | |
if result != nil | |
result[0] | |
else | |
result | |
end | |
end | |
it "valid one number expression" do | |
expect(complex_expression_match(" 1")).to eq(" 1") | |
expect(complex_expression_match("1 ")).to eq("1 ") | |
expect(complex_expression_match(" 1 ")).to eq(" 1 ") | |
expect(complex_expression_match(" y")).to eq(" y") | |
expect(complex_expression_match("y ")).to eq("y ") | |
expect(complex_expression_match(" y ")).to eq(" y ") | |
expect(complex_expression_match("0")).to eq("0") | |
expect(complex_expression_match("-0")).to eq("-0") | |
expect(complex_expression_match("+0")).to eq("+0") | |
expect(complex_expression_match("1")).to eq("1") | |
expect(complex_expression_match("-1")).to eq("-1") | |
expect(complex_expression_match("+1")).to eq("+1") | |
expect(complex_expression_match("01")).to eq("01") | |
expect(complex_expression_match("-01")).to eq("-01") | |
expect(complex_expression_match("+01")).to eq("+01") | |
expect(complex_expression_match("10")).to eq("10") | |
expect(complex_expression_match("-10")).to eq("-10") | |
expect(complex_expression_match("+10")).to eq("+10") | |
expect(complex_expression_match("(1)")).to eq("(1)") | |
expect(complex_expression_match("(-1)")).to eq("(-1)") | |
expect(complex_expression_match("(+1)")).to eq("(+1)") | |
expect(complex_expression_match("-(1)")).to eq("-(1)") | |
expect(complex_expression_match("+(1)")).to eq("+(1)") | |
expect(complex_expression_match("x")).to eq("x") | |
expect(complex_expression_match("-x")).to eq("-x") | |
expect(complex_expression_match("+x")).to eq("+x") | |
expect(complex_expression_match("(x)")).to eq("(x)") | |
expect(complex_expression_match("(-x)")).to eq("(-x)") | |
expect(complex_expression_match("(+x)")).to eq("(+x)") | |
end | |
it "invalid one number expression" do | |
expect(complex_expression_match("")).to eq(nil) | |
expect(complex_expression_match("0x")).to eq(nil) | |
expect(complex_expression_match("x0")).to eq(nil) | |
expect(complex_expression_match("0x0")).to eq(nil) | |
expect(complex_expression_match("x0x")).to eq(nil) | |
expect(complex_expression_match("-0x")).to eq(nil) | |
expect(complex_expression_match("-x0")).to eq(nil) | |
expect(complex_expression_match("-0x0")).to eq(nil) | |
expect(complex_expression_match("-x0x")).to eq(nil) | |
expect(complex_expression_match("+0x")).to eq(nil) | |
expect(complex_expression_match("+x0")).to eq(nil) | |
expect(complex_expression_match("+0x0")).to eq(nil) | |
expect(complex_expression_match("+x0x")).to eq(nil) | |
expect(complex_expression_match("(1")).to eq(nil) | |
expect(complex_expression_match("1)")).to eq(nil) | |
expect(complex_expression_match("(-1")).to eq(nil) | |
expect(complex_expression_match("-1)")).to eq(nil) | |
expect(complex_expression_match("(+1")).to eq(nil) | |
expect(complex_expression_match("+1)")).to eq(nil) | |
expect(complex_expression_match(")1")).to eq(nil) | |
expect(complex_expression_match("1(")).to eq(nil) | |
expect(complex_expression_match(")-1")).to eq(nil) | |
expect(complex_expression_match("-1(")).to eq(nil) | |
expect(complex_expression_match(")+1")).to eq(nil) | |
expect(complex_expression_match("+1(")).to eq(nil) | |
expect(complex_expression_match(")1(")).to eq(nil) | |
expect(complex_expression_match(")-1(")).to eq(nil) | |
expect(complex_expression_match(")+1(")).to eq(nil) | |
expect(complex_expression_match("-+1")).to eq(nil) | |
expect(complex_expression_match("+-1")).to eq(nil) | |
expect(complex_expression_match("- 1")).to eq(nil) | |
expect(complex_expression_match("+ 1")).to eq(nil) | |
expect(complex_expression_match("- -1")).to eq(nil) | |
expect(complex_expression_match("+ -1")).to eq(nil) | |
expect(complex_expression_match("- +1")).to eq(nil) | |
expect(complex_expression_match("+ +1")).to eq(nil) | |
expect(complex_expression_match("-1+")).to eq(nil) | |
expect(complex_expression_match("+1-")).to eq(nil) | |
expect(complex_expression_match("-1-")).to eq(nil) | |
expect(complex_expression_match("+1+")).to eq(nil) | |
expect(complex_expression_match("1 -")).to eq(nil) | |
expect(complex_expression_match("1-")).to eq(nil) | |
expect(complex_expression_match("1 +")).to eq(nil) | |
expect(complex_expression_match("1+")).to eq(nil) | |
expect(complex_expression_match("* 1")).to eq(nil) | |
expect(complex_expression_match("/ 1")).to eq(nil) | |
expect(complex_expression_match("1 *")).to eq(nil) | |
expect(complex_expression_match("1 /")).to eq(nil) | |
expect(complex_expression_match("*1")).to eq(nil) | |
expect(complex_expression_match("/1")).to eq(nil) | |
expect(complex_expression_match("1*")).to eq(nil) | |
expect(complex_expression_match("1/")).to eq(nil) | |
end | |
it "valid two numbers expression" do | |
expect(complex_expression_match("1 + 1")).to eq("1 + 1") | |
expect(complex_expression_match("1 +1")).to eq("1 +1") | |
expect(complex_expression_match("1+ 1")).to eq("1+ 1") | |
expect(complex_expression_match("1+1")).to eq("1+1") | |
expect(complex_expression_match("-1 + 1")).to eq("-1 + 1") | |
expect(complex_expression_match("-1 +1")).to eq("-1 +1") | |
expect(complex_expression_match("-1+ 1")).to eq("-1+ 1") | |
expect(complex_expression_match("-1+1")).to eq("-1+1") | |
expect(complex_expression_match("+1 + 1")).to eq("+1 + 1") | |
expect(complex_expression_match("+1 +1")).to eq("+1 +1") | |
expect(complex_expression_match("+1+ 1")).to eq("+1+ 1") | |
expect(complex_expression_match("+1+1")).to eq("+1+1") | |
expect(complex_expression_match("-1 + -1")).to eq("-1 + -1") | |
expect(complex_expression_match("-1 +-1")).to eq("-1 +-1") | |
expect(complex_expression_match("+1 + -1")).to eq("+1 + -1") | |
expect(complex_expression_match("+1 +-1")).to eq("+1 +-1") | |
expect(complex_expression_match("1 + 1")).to eq("1 + 1") | |
expect(complex_expression_match("1 - 1")).to eq("1 - 1") | |
expect(complex_expression_match("1 * 1")).to eq("1 * 1") | |
expect(complex_expression_match("1 / 1")).to eq("1 / 1") | |
expect(complex_expression_match("1+1")).to eq("1+1") | |
expect(complex_expression_match("1-1")).to eq("1-1") | |
expect(complex_expression_match("1*1")).to eq("1*1") | |
expect(complex_expression_match("1/1")).to eq("1/1") | |
expect(complex_expression_match("(1 + 1)")).to eq("(1 + 1)") | |
expect(complex_expression_match("(1 - 1)")).to eq("(1 - 1)") | |
expect(complex_expression_match("(1 * 1)")).to eq("(1 * 1)") | |
expect(complex_expression_match("(1 / 1)")).to eq("(1 / 1)") | |
expect(complex_expression_match("(1+1)")).to eq("(1+1)") | |
expect(complex_expression_match("(1-1)")).to eq("(1-1)") | |
expect(complex_expression_match("(1*1)")).to eq("(1*1)") | |
expect(complex_expression_match("(1/1)")).to eq("(1/1)") | |
expect(complex_expression_match("-(1+1)")).to eq("-(1+1)") | |
expect(complex_expression_match("+(1+1)")).to eq("+(1+1)") | |
end | |
it "invalid two numbers expression" do | |
expect(complex_expression_match("1 // 1")).to eq(nil) | |
expect(complex_expression_match("1 ** 1")).to eq(nil) | |
expect(complex_expression_match("1 1")).to eq(nil) | |
end | |
it "valid three numbers expression" do | |
expect(complex_expression_match("1 + 1 + 1")).to eq("1 + 1 + 1") | |
expect(complex_expression_match("1 - 1 - 1")).to eq("1 - 1 - 1") | |
expect(complex_expression_match("(1) + (1) + (1)")).to eq("(1) + (1) + (1)") | |
expect(complex_expression_match("(1) - (1) - (1)")).to eq("(1) - (1) - (1)") | |
expect(complex_expression_match("(1 + 1 + 1)")).to eq("(1 + 1 + 1)") | |
expect(complex_expression_match("(1 - (1 - 1))")).to eq("(1 - (1 - 1))") | |
expect(complex_expression_match("((1 - 1) - 1)")).to eq("((1 - 1) - 1)") | |
end | |
it "invalid three numbers expression" do | |
expect(complex_expression_match("1 // 1 1")).to eq(nil) | |
expect(complex_expression_match("1 ** 1 1")).to eq(nil) | |
expect(complex_expression_match("1 1 1")).to eq(nil) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The body of convert_for_exact_match should be | |
# self.wrap('\A', '\z') | |
# but I want edit it. Who knows, it might be considered | |
# as cheating! | |
# Add to Regex | |
class Regexp | |
def +(re) | |
Regexp.new self.to_s + re.to_s | |
end | |
def wrap(before, after) | |
Regexp.new (before.to_s + self.to_s + after.to_s) | |
end | |
def convert_for_exact_match | |
self.wrap('^', '$') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment