1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
package hm
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSimpleEnv(t *testing.T) {
assert := assert.New(t)
var orig, env Env
var expected SimpleEnv
// Add
orig = make(SimpleEnv)
orig = orig.Add("foo", NewScheme(
TypeVarSet{'a', 'b', 'c'},
TypeVariable('a'),
))
orig = orig.Add("bar", NewScheme(
TypeVarSet{'b', 'c', 'd'},
TypeVariable('a'),
))
orig = orig.Add("baz", NewScheme(
TypeVarSet{'a', 'b', 'c'},
neutron,
))
qs := NewScheme(
TypeVarSet{'a', 'b'},
proton,
)
orig = orig.Add("qux", qs)
expected = SimpleEnv{
"foo": NewScheme(
TypeVarSet{'a', 'b', 'c'},
TypeVariable('a'),
),
"bar": NewScheme(
TypeVarSet{'b', 'c', 'd'},
TypeVariable('a'),
),
"baz": NewScheme(
TypeVarSet{'a', 'b', 'c'},
neutron,
),
"qux": NewScheme(
TypeVarSet{'a', 'b'},
proton,
),
}
assert.Equal(expected, orig)
// Get
s, ok := orig.SchemeOf("qux")
if s != qs || !ok {
t.Error("Expected to get scheme of \"qux\"")
}
// Remove
orig = orig.Remove("qux")
delete(expected, "qux")
assert.Equal(expected, orig)
// Clone
env = orig.Clone()
assert.Equal(orig, env)
subs := mSubs{
'a': proton,
'b': neutron,
'd': electron,
'e': proton,
}
env = env.Apply(subs).(Env)
expected = SimpleEnv{
"foo": &Scheme{
tvs: TypeVarSet{'a', 'b', 'c'},
t: TypeVariable('a'),
},
"bar": &Scheme{
tvs: TypeVarSet{'b', 'c', 'd'},
t: proton,
},
"baz": &Scheme{
tvs: TypeVarSet{'a', 'b', 'c'},
t: neutron,
},
}
assert.Equal(expected, env)
env = orig.Clone()
ftv := env.FreeTypeVar()
correctFTV := TypeVarSet{'a'}
if !correctFTV.Equals(ftv) {
t.Errorf("Expected freetypevars to be equal. Got %v instead", ftv)
}
}
|