Added analyzer tests for constants

This commit is contained in:
Sasha Koshka 2024-04-07 23:47:30 -04:00
parent e12e83921b
commit 6313992c10
3 changed files with 135 additions and 0 deletions

View File

@ -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
}
`)
}

90
analyzer/constant_test.go Normal file
View File

@ -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]
`)}

View File

@ -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'
`)}