diff --git a/analyzer/assignment_test.go b/analyzer/assignment_test.go index 5353cd6..8b6c036 100644 --- a/analyzer/assignment_test.go +++ b/analyzer/assignment_test.go @@ -321,3 +321,15 @@ B:(& [g]:Int) } `) } + +func TestAssignmentErrConstant (test *testing.T) { +testStringErr (test, +"cannot assign to constant", 5, 2, +` +T: Int +| x 0 +[main] = { + T.x = 5 +} +`) +} diff --git a/analyzer/constant_test.go b/analyzer/constant_test.go new file mode 100644 index 0000000..0b59f6d --- /dev/null +++ b/analyzer/constant_test.go @@ -0,0 +1,90 @@ +package analyzer + +import "testing" + +func TestConstantValueSpecified (test *testing.T) { +testString (test, +` +Weekday: Int +| sunday 1 +| monday 2 +| tuesday 3 +| wednesday 4 +| thursday 5 +| friday 6 +| saturday 7 + +[print s:String] +[printWeekday w:Weekday] = [print switch w + | sunday 'sunday' + | monday 'monday' + | tuesday 'tuesday' + | wednesday 'wednesday' + | thursday 'thursday' + | friday 'friday' + | saturday 'saturday' + * 'unknown'] +[f] = [printWeekday Weekday.monday] +`)} + +func TestConstantValueUnspecified (test *testing.T) { +testString (test, +` +Weekday: Int +| sunday +| monday +| tuesday +| wednesday +| thursday +| friday +| saturday + +[print s:String] +[printWeekday w:Weekday] = [print switch w + | sunday 'sunday' + | monday 'monday' + | tuesday 'tuesday' + | wednesday 'wednesday' + | thursday 'thursday' + | friday 'friday' + | saturday 'saturday' + * 'unknown'] +[f] = [printWeekday Weekday.monday] +`)} + +func TestConstantString (test *testing.T) { +testString (test, +` +Weekday: String +| sunday 'sunday' +| monday 'monday' +| tuesday 'tuesday' +| wednesday 'wednesday' +| thursday 'thursday' +| friday 'friday' +| saturday 'saturday' + +[print s:String] +[printWeekday w:Weekday] = [print [~String w]] +[f] = [printWeekday Weekday.monday] +`)} + +// TODO: consider filling values for string constants by using the constant +// name? +func TestErrConstantStringUnspecified (test *testing.T) { +testStringErr (test, +"cannot fill in constant values for non-numeric type Weekday", 5, 11 +` +Weekday: String +| sunday +| monday +| tuesday +| wednesday +| thursday +| friday +| saturday + +[print s:String] +[printWeekday w:Weekday] = [print [~String w]] +[f] = [printWeekday Weekday.monday] +`)} diff --git a/analyzer/name_test.go b/analyzer/name_test.go index 82c5ed8..f0c0711 100644 --- a/analyzer/name_test.go +++ b/analyzer/name_test.go @@ -203,3 +203,36 @@ Type: (. world:Int) } `) } + +func TestConstantNameErrMissing (test *testing.T) { +testStringErr (test, +"no constant Weekday.palday", 13, 21, +` +Weekday: String +| sunday 'sunday' +| monday 'monday' +| tuesday 'tuesday' +| wednesday 'wednesday' +| thursday 'thursday' +| friday 'friday' +| saturday 'saturday' + +[print s:String] +[printWeekday w:Weekday] = [print [~String w]] +[f] = [printWeekday Weekday.palday] +`)} + +func TestConstantUniqueErr (test *testing.T) { +testStringErr (test, +"Weekday.tuesday already defined at stream0.fspl:5:1", 8, 1, +` +Weekday: String +| sunday 'sunday' +| monday 'monday' +| tuesday 'tuesday' +| wednesday 'wednesday' +| thursday 'thursday' +| tuesday 'tuesday' +| friday 'friday' +| saturday 'saturday' +`)}