| package repl |
| |
| import ( |
| "bytes" |
| "strings" |
| "testing" |
| ) |
| |
| func TestRun_SingleExpression(t *testing.T) { |
| in := strings.NewReader("1 + 2\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "3") { |
| t.Errorf("expected output to contain '3', got %q", got) |
| } |
| } |
| |
| func TestRun_MultipleExpressions(t *testing.T) { |
| in := strings.NewReader("1 + 2\n3 * 4\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "3") { |
| t.Errorf("expected output to contain '3', got %q", got) |
| } |
| if !strings.Contains(got, "12") { |
| t.Errorf("expected output to contain '12', got %q", got) |
| } |
| } |
| |
| func TestRun_ErrorContinues(t *testing.T) { |
| // First line has error, second is valid. |
| in := strings.NewReader("1 +\n2 + 3\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "error:") { |
| t.Errorf("expected output to contain 'error:', got %q", got) |
| } |
| if !strings.Contains(got, "5") { |
| t.Errorf("expected output to contain '5' after error recovery, got %q", got) |
| } |
| } |
| |
| func TestRun_DivisionByZero(t *testing.T) { |
| in := strings.NewReader("1 / 0\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "error:") { |
| t.Errorf("expected output to contain 'error:', got %q", got) |
| } |
| if !strings.Contains(got, "division by zero") { |
| t.Errorf("expected 'division by zero' in output, got %q", got) |
| } |
| } |
| |
| func TestRun_EmptyLine(t *testing.T) { |
| // Empty lines should be skipped, not cause errors. |
| in := strings.NewReader("\n1 + 1\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "2") { |
| t.Errorf("expected output to contain '2', got %q", got) |
| } |
| // Should not contain any error. |
| if strings.Contains(got, "error:") { |
| t.Errorf("empty line should not produce error, got %q", got) |
| } |
| } |
| |
| func TestRun_Prompt(t *testing.T) { |
| in := strings.NewReader("42\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, ">> ") { |
| t.Errorf("expected prompt '>> ' in output, got %q", got) |
| } |
| } |
| |
| func TestRun_FloatResult(t *testing.T) { |
| in := strings.NewReader("7 / 2\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "3.5") { |
| t.Errorf("expected output to contain '3.5', got %q", got) |
| } |
| } |
| |
| func TestRun_InvalidCharacter(t *testing.T) { |
| in := strings.NewReader("1 @ 2\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| if !strings.Contains(got, "error:") { |
| t.Errorf("expected output to contain 'error:', got %q", got) |
| } |
| } |
| |
| func TestRun_EmptyInput(t *testing.T) { |
| // No input at all — just EOF. |
| in := strings.NewReader("") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| // Should just show the prompt and exit gracefully. |
| if !strings.Contains(got, ">> ") { |
| t.Errorf("expected at least one prompt, got %q", got) |
| } |
| } |
| |
| func TestRun_WholeIntegerNoTrailingZeros(t *testing.T) { |
| // 2 + 3 = 5, should print "5" not "5.000000". |
| in := strings.NewReader("2 + 3\n") |
| var out bytes.Buffer |
| |
| Run(in, &out) |
| |
| got := out.String() |
| // Result line is "5\n" (between prompts). |
| if !strings.Contains(got, "5\n") { |
| t.Errorf("expected '5\\n' in output, got %q", got) |
| } |
| // Should not contain "5.0" |
| if strings.Contains(got, "5.0") { |
| t.Errorf("expected no trailing zeros, got %q", got) |
| } |
| } |