Skip to content

Commit

Permalink
types: support isEquals
Browse files Browse the repository at this point in the history
  • Loading branch information
rpbeltran committed Jan 4, 2025
1 parent 3832b37 commit f7ecf14
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
9 changes: 9 additions & 0 deletions types/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ func MakeBool(value bool) *PrimitiveBool {
return &PrimitiveBool{value}
}

// Comparisons

func (boolean *PrimitiveBool) Equal(other Primitive) bool {
if as_bool, err := other.RequireBool(); err == nil {
return boolean.value == as_bool.value
}
return false
}

// Logical Operators

func (boolean *PrimitiveBool) And(other Primitive) (Primitive, error) {
Expand Down
30 changes: 29 additions & 1 deletion types/boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestBoolDisplay(t *testing.T) {
}
}

func TestTruthy(t *testing.T) {
func TestBoolTruthy(t *testing.T) {
if MakeBool(false).Truthy() {
t.Fatalf("MakeBool(false).Truthy() gave true, expected false")
}
Expand All @@ -227,3 +227,31 @@ func TestTruthy(t *testing.T) {
t.Fatalf("MakeBool(true).Truthy() gave false, expected true")
}
}

func TestBoolEquality(t *testing.T) {
if !MakeBool(false).Equal(MakeBool(false)) {
t.Fatal("Equal(false, false) gave false, expected true")
}
if MakeBool(false).Equal(MakeBool(true)) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeBool(true).Equal(MakeBool(false)) {
t.Fatal("Equal(true, false) gave true, expected false")
}
if !MakeBool(true).Equal(MakeBool(true)) {
t.Fatal("Equal(true, true) gave false, expected true")
}

if MakeBool(false).Equal(MakeString("")) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeBool(true).Equal(MakeString("")) {
t.Fatal("Equal(true, false) gave true, expected false")
}
if MakeBool(false).Equal(makeNumOrFail("0", t)) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeBool(true).Equal(makeNumOrFail("1", t)) {
t.Fatal("Equal(true, false) gave true, expected false")
}
}
9 changes: 9 additions & 0 deletions types/numbers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func MakeNumber(value string) (*PrimitiveNum, error) {
return &PrimitiveNum{num}, nil
}

// Comparisons

func (num *PrimitiveNum) Equal(other Primitive) bool {
if as_num, err := other.RequireNum(); err == nil {
return num.value.Cmp(&as_num.value) == 0
}
return false
}

// Operators

func (num *PrimitiveNum) Add(other Primitive) (Primitive, error) {
Expand Down
17 changes: 17 additions & 0 deletions types/numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,20 @@ func TestNumTruthy(t *testing.T) {
t.Fatalf("PrimitiveNum(0).Truthy() returned true, expected false")
}
}

func TestNumEquality(t *testing.T) {
if !makeNumOrFail("1", t).Equal(makeNumOrFail("1.0", t)) {
t.Fatal("Equal(1, 1) gave false, expected true")
}
if makeNumOrFail("1", t).Equal(makeNumOrFail("2", t)) {
t.Fatal("Equal(1, 2) gave true, expected false")
}

if makeNumOrFail("1", t).Equal(MakeBool(true)) {
t.Fatal("Equal(1, true) gave true, expected false")
}
if makeNumOrFail("1", t).Equal(MakeString("1")) {
t.Fatal(`Equal("1", 1) gave true, expected false`)
}

}
9 changes: 9 additions & 0 deletions types/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ func MakeString(value string) *PrimitiveStr {
return &PrimitiveStr{value}
}

// Comparisons

func (str *PrimitiveStr) Equal(other Primitive) bool {
if as_str, err := other.RequireStr(); err == nil {
return str.value == as_str.value
}
return false
}

// String Operators

func (str *PrimitiveStr) ConcatenateInPlace(other Primitive) error {
Expand Down
28 changes: 28 additions & 0 deletions types/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,31 @@ func TestStrTruthy(t *testing.T) {
t.Fatal(`PrimitiveStr("").Truthy() returned true, expected false`)
}
}

func TestStringEquality(t *testing.T) {
if !MakeString("foo").Equal(MakeString("foo")) {
t.Fatal("Equal(false, false) gave false, expected true")
}
if MakeString("foo").Equal(MakeString("bar")) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeString("bar").Equal(MakeString("foo")) {
t.Fatal("Equal(true, false) gave true, expected false")
}
if !MakeString("bar").Equal(MakeString("bar")) {
t.Fatal("Equal(true, true) gave false, expected true")
}

if MakeString("true").Equal(MakeBool(true)) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeString("").Equal(MakeBool(false)) {
t.Fatal("Equal(true, false) gave true, expected false")
}
if MakeString("0").Equal(makeNumOrFail("0", t)) {
t.Fatal("Equal(false, true) gave true, expected false")
}
if MakeString("1").Equal(makeNumOrFail("1", t)) {
t.Fatal("Equal(true, false) gave true, expected false")
}
}
3 changes: 3 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Primitive interface {
And(other Primitive) (Primitive, error)
Or(other Primitive) (Primitive, error)

// - Comparisons
Equal(other Primitive) bool

// Casting
RequireNum() (*PrimitiveNum, error)
RequireStr() (*PrimitiveStr, error)
Expand Down

0 comments on commit f7ecf14

Please sign in to comment.