mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Generating strings and comparing them to implement Equal is a quick and easy solution. Unfortunately when this code is in the hot path, it becomes very expensive, so this commit changes some of those instances to compare the values directly. Combined with using addr.Equal instead of checking for string equality, this makes Terraform dramatically faster for some operations, such as generating large JSON plans.
58 lines
942 B
Go
58 lines
942 B
Go
package addrs
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestModuleEqual_true(t *testing.T) {
|
|
modules := []Module{
|
|
RootModule,
|
|
{"a"},
|
|
{"a", "b"},
|
|
{"a", "b", "c"},
|
|
}
|
|
for _, m := range modules {
|
|
t.Run(m.String(), func(t *testing.T) {
|
|
if !m.Equal(m) {
|
|
t.Fatalf("expected %#v to be equal to itself", m)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestModuleEqual_false(t *testing.T) {
|
|
testCases := []struct {
|
|
left Module
|
|
right Module
|
|
}{
|
|
{
|
|
RootModule,
|
|
Module{"a"},
|
|
},
|
|
{
|
|
Module{"a"},
|
|
Module{"b"},
|
|
},
|
|
{
|
|
Module{"a"},
|
|
Module{"a", "a"},
|
|
},
|
|
{
|
|
Module{"a", "b"},
|
|
Module{"a", "B"},
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
|
|
if tc.left.Equal(tc.right) {
|
|
t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
|
|
}
|
|
|
|
if tc.right.Equal(tc.left) {
|
|
t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
|
|
}
|
|
})
|
|
}
|
|
}
|