aboutsummaryrefslogtreecommitdiff
path: root/evaluator/quote_unquote_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'evaluator/quote_unquote_test.go')
-rw-r--r--evaluator/quote_unquote_test.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/evaluator/quote_unquote_test.go b/evaluator/quote_unquote_test.go
new file mode 100644
index 0000000..2859540
--- /dev/null
+++ b/evaluator/quote_unquote_test.go
@@ -0,0 +1,117 @@
+package evaluator
+
+import (
+ "monkey/object"
+ "testing"
+)
+
+func TestQuote(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {
+ `quote(5)`,
+ `5`,
+ },
+ {
+ `quote(5 + 8)`,
+ `(5 + 8)`,
+ },
+ {
+ `quote(foobar)`,
+ `foobar`,
+ },
+ {
+ `quote(foobar + barfoo)`,
+ `(foobar + barfoo)`,
+ },
+ }
+
+ for _, tt := range tests {
+ evaluated := testEval(tt.input)
+ quote, ok := evaluated.(*object.Quote)
+ if !ok {
+ t.Fatalf("expected *object.Quote. got=%T (%+v)",
+ evaluated, evaluated)
+ }
+
+ if quote.Node == nil {
+ t.Fatalf("quote.Node is nil")
+ }
+
+ if quote.Node.String() != tt.expected {
+ t.Errorf("not equal. got=%q, want=%q",
+ quote.Node.String(), tt.expected)
+ }
+ }
+}
+
+func TestQuoteUnquote(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ {
+ `quote(unquote(4))`,
+ `4`,
+ },
+ {
+ `quote(unquote(4 + 4))`,
+ `8`,
+ },
+ {
+ `quote(8 + unquote(4 + 4))`,
+ `(8 + 8)`,
+ },
+ {
+ `quote(unquote(4 + 4) + 8)`,
+ `(8 + 8)`,
+ },
+ {
+ `let foobar = 8;
+ quote(foobar)`,
+ `foobar`,
+ },
+ {
+ `let foobar = 8;
+ quote(unquote(foobar))`,
+ `8`,
+ },
+ {
+ `quote(unquote(true))`,
+ `true`,
+ },
+ {
+ `quote(unquote(true == false))`,
+ `false`,
+ },
+ {
+ `quote(unquote(quote(4 + 4)))`,
+ `(4 + 4)`,
+ },
+ {
+ `let quotedInfixExpression = quote(4 + 4);
+ quote(unquote(4 + 4) + unquote(quotedInfixExpression))`,
+ `(8 + (4 + 4))`,
+ },
+ }
+
+ for _, tt := range tests {
+ evaluated := testEval(tt.input)
+ quote, ok := evaluated.(*object.Quote)
+ if !ok {
+ t.Fatalf("expected *object.Quote. got=%T (%+v)",
+ evaluated, evaluated)
+ }
+
+ if quote.Node == nil {
+ t.Fatalf("quote.Node is nil")
+ }
+
+ if quote.Node.String() != tt.expected {
+ t.Errorf("not equal. got=%q, want=%q",
+ quote.Node.String(), tt.expected)
+ }
+ }
+}