<commit_before>package dexpr

import (
	"errors"
	"fmt"
	"github.com/lawrencewoodman/dlit"
	"math"
	"testing"
)

func TestMustNew(t *testing.T) {
	cases := []struct {
		in      string
		wantStr string
	}{
		{"a+b", "a+b"},
		{"income", "income"},
		{"6.6", "6.6"},
	}

	for _, c := range cases {
		got := MustNew(c.in)
		if got.String() != c.wantStr {
			t.Errorf("MustNew(%q) - got: %s, want: %s", c.in, got, c.wantStr)
		}
	}
}

func TestMustNew_panic(t *testing.T) {
	cases := []struct {
		in        string
		wantPanic string
	}{
		{"/bob harry", ErrInvalidExpr("Invalid expression: /bob harry").Error()},
	}

	for _, c := range cases {
		paniced := false
		defer func() {
			if r := recover(); r != nil {
				if r.(string) == c.wantPanic {
					paniced = true
				} else {
					t.Errorf("MustNew(%q) - got panic: %s, wanted: %s",
						c.in, r, c.wantPanic)
				}
			}
		}()
		MustNew(c.in)
		if c.wantPanic != "" && !paniced {
			t.Errorf("MustNew(%q) - failed to panic with: %s", c.in, c.wantPanic)
		}
	}
}

func TestNew_errors(t *testing.T) {
	cases := []struct {
		in        string
		wantError error
	}{
		{"7 {} 3", ErrInvalidExpr("Invalid expression: 7 {} 3")},
	}
	for _, c := range cases {
		_, err := New(c.in)
		if err == nil {
			t.Errorf("New(%s) no error, wanted: %s", c.in, err)
		}
		if err.Error() != c.wantError.Error() {
			t.Errorf("New(%s) got error: %s, wanted: %s", c.in, err, c.wantError)
		}
	}
}

func TestEval_noerrors(t *testing.T) {
	cases := []struct {
		in   string
		want *dlit.Literal
	}{
		{"1 == 1", dlit.MustNew(true)},
		{"1 == 2", dlit.MustNew(false)},
		{"2.6 + 2.5", dlit.MustNew(5.1)},
		{"-2 + -2", dlit.MustNew(-4)},
		{"-2.5 + -2.6", dlit.MustNew(-5.1)},
		{"-2 - 3", dlit.MustNew(-5)},
		{"-2.5 - 3.6", dlit.MustNew(-6.1)},
		{"8 - 9", dlit.MustNew(-1)},
		{"a + numStrB", dlit.MustNew(7)},
		{"8/4", dlit.MustNew(2)},
		{"1/4", dlit.MustNew(0.25)},<commit_msg>Add further error test for New<commit_after>package dexpr

import (
	"errors"
	"fmt"
	"github.com/lawrencewoodman/dlit"
	"math"
	"testing"
)

func TestMustNew(t *testing.T) {
	cases := []struct {
		in      string
		wantStr string
	}{
		{"a+b", "a+b"},
		{"income", "income"},
		{"6.6", "6.6"},
	}

	for _, c := range cases {
		got := MustNew(c.in)
		if got.String() != c.wantStr {
			t.Errorf("MustNew(%q) - got: %s, want: %s", c.in, got, c.wantStr)
		}
	}
}

func TestMustNew_panic(t *testing.T) {
	cases := []struct {
		in        string
		wantPanic string
	}{
		{"/bob harry", ErrInvalidExpr("Invalid expression: /bob harry").Error()},
	}

	for _, c := range cases {
		paniced := false
		defer func() {
			if r := recover(); r != nil {
				if r.(string) == c.wantPanic {
					paniced = true
				} else {
					t.Errorf("MustNew(%q) - got panic: %s, wanted: %s",
						c.in, r, c.wantPanic)
				}
			}
		}()
		MustNew(c.in)
		if c.wantPanic != "" && !paniced {
			t.Errorf("MustNew(%q) - failed to panic with: %s", c.in, c.wantPanic)
		}
	}
}

func TestNew_errors(t *testing.T) {
	cases := []struct {
		in        string
		wantError error
	}{
		{"7 {} 3", ErrInvalidExpr("Invalid expression: 7 {} 3")},
		{"8/cot££t", ErrInvalidExpr("Invalid expression: 8/cot££t")},
	}
	for _, c := range cases {
		_, err := New(c.in)
		if err == nil {
			t.Errorf("New(%s) no error, wanted: %s", c.in, err)
		}
		if err.Error() != c.wantError.Error() {
			t.Errorf("New(%s) got error: %s, wanted: %s", c.in, err, c.wantError)
		}
	}
}

func TestEval_noerrors(t *testing.T) {
	cases := []struct {
		in   string
		want *dlit.Literal
	}{
		{"1 == 1", dlit.MustNew(true)},
		{"1 == 2", dlit.MustNew(false)},
		{"2.6 + 2.5", dlit.MustNew(5.1)},
		{"-2 + -2", dlit.MustNew(-4)},
		{"-2.5 + -2.6", dlit.MustNew(-5.1)},
		{"-2 - 3", dlit.MustNew(-5)},
		{"-2.5 - 3.6", dlit.MustNew(-6.1)},
		{"8 - 9", dlit.MustNew(-1)},
		{"a + numStrB", dlit.MustNew(7)},
		{"8/4", dlit.MustNew(2)},
		{"1/4", dlit.MustNew(0.25)},
{'fraction_non_alphanumeric': 0.2304537298128685, 'fraction_numerical': 0.025121763650346064, 'mean_word_length': 2.7957198443579765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1490883', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1676, 'n_words': 364}