Files
mgmt/lang/scope_test.go
James Shubin f53376cea1 lang: Add function values and lambdas
This adds a giant missing piece of the language: proper function values!
It is lovely to now understand why early programming language designers
didn't implement these, but a joy to now reap the benefits of them. In
adding these, many other changes had to be made to get them to "fit"
correctly. This improved the code and fixed a number of bugs.
Unfortunately this touched many areas of the code, and since I was
learning how to do all of this for the first time, I've squashed most of
my work into a single commit. Some more information:

* This adds over 70 new tests to verify the new functionality.

* Functions, global variables, and classes can all be implemented
natively in mcl and built into core packages.

* A new compiler step called "Ordering" was added. It is called by the
SetScope step, and determines statement ordering and shadowing
precedence formally. It helped remove at least one bug and provided the
additional analysis required to properly capture variables when
implementing function generators and closures.

* The type unification code was improved to handle the new cases.

* Light copying of Node's allowed our function graphs to be more optimal
and share common vertices and edges. For example, if two different
closures capture a variable $x, they'll both use the same copy when
running the function, since the compiler can prove if they're identical.

* Some areas still need improvements, but this is ready for mainstream
testing and use!
2019-07-17 00:27:09 -04:00

180 lines
3.8 KiB
Go

// Mgmt
// Copyright (C) 2013-2019+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !root
package lang
import (
"fmt"
"reflect"
"testing"
"github.com/purpleidea/mgmt/lang/interfaces"
"github.com/purpleidea/mgmt/util"
)
func TestScopeIndexesPush0(t *testing.T) {
type test struct { // an individual test
name string
indexes map[int][]interfaces.Expr
pushed []interfaces.Expr
expected map[int][]interfaces.Expr
}
testCases := []test{}
//{
// testCases = append(testCases, test{
// name: "empty",
// pushed: nil, // TODO: undefined, but should we do it?
// expected: map[int][]interfaces.Expr{
// 0: {}, // empty list ?
// },
// })
//}
{
testCases = append(testCases, test{
name: "empty list",
pushed: []interfaces.Expr{}, // empty list
expected: map[int][]interfaces.Expr{
0: {}, // empty list
},
})
}
{
b1 := &ExprBool{}
b2 := &ExprBool{}
b3 := &ExprBool{}
b4 := &ExprBool{}
b5 := &ExprBool{}
b6 := &ExprBool{}
b7 := &ExprBool{}
b8 := &ExprBool{}
testCases = append(testCases, test{
name: "simple push",
indexes: map[int][]interfaces.Expr{
0: {
b1, b2, b3,
},
1: {
b4,
},
2: {
b5, b6,
},
},
pushed: []interfaces.Expr{
b7, b8,
},
expected: map[int][]interfaces.Expr{
0: {
b7, b8,
},
1: {
b1, b2, b3,
},
2: {
b4,
},
3: {
b5, b6,
},
},
})
}
{
b1 := &ExprBool{}
b2 := &ExprBool{}
b3 := &ExprBool{}
b4 := &ExprBool{}
b5 := &ExprBool{}
b6 := &ExprBool{}
b7 := &ExprBool{}
b8 := &ExprBool{}
testCases = append(testCases, test{
name: "push with gaps",
indexes: map[int][]interfaces.Expr{
0: {
b1, b2, b3,
},
// there is a gap here
2: {
b4,
},
3: {
b5, b6,
},
},
pushed: []interfaces.Expr{
b7, b8,
},
expected: map[int][]interfaces.Expr{
0: {
b7, b8,
},
// the gap remains
1: {
b1, b2, b3,
},
3: {
b4,
},
4: {
b5, b6,
},
},
})
}
names := []string{}
for index, tc := range testCases { // run all the tests
if tc.name == "" {
t.Errorf("test #%d: not named", index)
continue
}
if util.StrInList(tc.name, names) {
t.Errorf("test #%d: duplicate sub test name of: %s", index, tc.name)
continue
}
names = append(names, tc.name)
//if index != 3 { // hack to run a subset (useful for debugging)
//if (index != 20 && index != 21) {
//if tc.name != "nil" {
// continue
//}
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
name, indexes, pushed, expected := tc.name, tc.indexes, tc.pushed, tc.expected
t.Logf("\n\ntest #%d (%s) ----------------\n\n", index, name)
scope := &interfaces.Scope{
Indexes: indexes,
}
scope.PushIndexes(pushed)
out := scope.Indexes
if !reflect.DeepEqual(out, expected) {
t.Errorf("test #%d: indexes did not match expected", index)
t.Logf("test #%d: actual: \n\n%+v\n", index, out)
t.Logf("test #%d: expected: \n\n%+v", index, expected)
return
}
})
}
}