aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-11-04 12:33:10 +0100
committerAnhgelus Morhtuuzh <william@herges.fr>2025-11-04 12:33:10 +0100
commit3a25de68dfe4832230ec952dd5395191e3638162 (patch)
tree714b63c35ef147048e5dab8a4df5e694fc2f7be3
parent4ac065fbe57ed19760dbb55453e5875b3c0c188d (diff)
test(): default
-rw-r--r--example/input.txt8
-rw-r--r--test/elixir_math_parser_test.exs59
2 files changed, 65 insertions, 2 deletions
diff --git a/example/input.txt b/example/input.txt
new file mode 100644
index 0000000..cb3073b
--- /dev/null
+++ b/example/input.txt
@@ -0,0 +1,8 @@
+:a = 7
+:b = 4
+:result = :a + :b * 10 / 2
+
+:x = 20
+:y = 3
+:z = 6
+:value = 10 + :z / :y * :x / 4
diff --git a/test/elixir_math_parser_test.exs b/test/elixir_math_parser_test.exs
index 23f1048..3ccfb94 100644
--- a/test/elixir_math_parser_test.exs
+++ b/test/elixir_math_parser_test.exs
@@ -1,8 +1,63 @@
defmodule ElixirMathParserTest do
use ExUnit.Case
+ import ElixirMathParser, only: [parse_and_eval: 1]
doctest ElixirMathParser
- test "greets the world" do
- assert ElixirMathParser.hello() == :world
+ test "assignment" do
+ assert parse_and_eval(":a = 1") == %{a: 1}
+ end
+
+ test "add" do
+ assert parse_and_eval(":a = 3 + 2") == %{a: 5}
+ end
+
+ test "sub" do
+ assert parse_and_eval(":a = 3 - 2") == %{a: 1}
+ end
+
+ test "mul" do
+ assert parse_and_eval(":a = 3 * 2") == %{a: 6}
+ end
+
+ test "div" do
+ assert parse_and_eval(":a = 6 / 2") == %{a: 3}
+ end
+
+ test "operator precedence for mul" do
+ assert parse_and_eval(":a = 3 * 2 + 1") == %{a: 7}
+ assert parse_and_eval(":a = 3 * 2 - 1") == %{a: 5}
+ assert parse_and_eval(":a = 1 + 3 * 2") == %{a: 7}
+ assert parse_and_eval(":a = 1 - 3 * 2") == %{a: -5}
+ end
+
+ test "operator precedence for div" do
+ assert parse_and_eval(":a = 6 / 2 + 1") == %{a: 4}
+ assert parse_and_eval(":a = 6 / 2 - 1") == %{a: 2}
+ assert parse_and_eval(":a = 1 + 6 / 2") == %{a: 4}
+ assert parse_and_eval(":a = 1 - 6 / 2") == %{a: -2}
+ end
+
+ test "variable reference" do
+ assert parse_and_eval("""
+ :a = 3
+ :b = 2
+ :result = :a + :b
+ """) == %{a: 3, b: 2, result: 5}
+ end
+
+ test "variable update" do
+ assert parse_and_eval("""
+ :a = 3
+ :b = 2
+ :a = :a + :b
+ """) == %{a: 5, b: 2}
+ end
+
+ test "variables with numbers in name" do
+ assert parse_and_eval("""
+ :a1 = 3
+ :a2 = 2
+ :b1 = :a1 + :a2
+ """) == %{a1: 3, a2: 2, b1: 5}
end
end