github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/test/test.go (about)

     1  package test
     2  
     3  import (
     4  	"embed"
     5  	"io/fs"
     6  	"testing"
     7  
     8  	"github.com/madlambda/spells/assert"
     9  	"github.com/mineiros-io/terradoc/internal/entities"
    10  	"github.com/mineiros-io/terradoc/internal/validators"
    11  )
    12  
    13  //go:embed testdata/*
    14  var testDataFS embed.FS
    15  
    16  func ReadFixture(t *testing.T, filename string) []byte {
    17  	data, err := testDataFS.ReadFile("testdata/" + filename)
    18  	assert.NoError(t, err)
    19  
    20  	return data
    21  }
    22  
    23  func OpenFixture(t *testing.T, filename string) fs.File {
    24  	f, err := testDataFS.Open("testdata/" + filename)
    25  	assert.NoError(t, err)
    26  
    27  	return f
    28  }
    29  
    30  func AssertEqualTypes(t *testing.T, want, got entities.Type) {
    31  	t.Helper()
    32  
    33  	assert.EqualStrings(t, want.TFType.String(), got.TFType.String())
    34  	assert.EqualStrings(t, want.Label, got.Label)
    35  
    36  	if want.Nested != nil {
    37  		assert.EqualStrings(t, want.Nested.TFType.String(), got.Nested.TFType.String())
    38  		assert.EqualStrings(t, want.Nested.Label, got.Nested.Label)
    39  	}
    40  }
    41  
    42  func AssertHasTypeMismatches(t *testing.T, want, got []validators.TypeMismatchResult) {
    43  	for _, tm := range want {
    44  		found := false
    45  
    46  		for _, tms := range got {
    47  			if tms.Name == tm.Name {
    48  				found = true
    49  
    50  				assert.EqualStrings(t, tm.DefinedType, tms.DefinedType)
    51  				assert.EqualStrings(t, tm.DocumentedType, tms.DocumentedType)
    52  			}
    53  		}
    54  
    55  		if !found {
    56  			t.Errorf("wanted %q to be found in %v", tm.Name, got)
    57  		}
    58  	}
    59  }
    60  
    61  func AssertHasStrings(t *testing.T, want, got []string) {
    62  	t.Helper()
    63  
    64  	for _, name := range want {
    65  		found := false
    66  
    67  		for _, o := range got {
    68  			if name == o {
    69  				found = true
    70  			}
    71  		}
    72  
    73  		if !found {
    74  			t.Errorf("wanted %q to be found in %v", name, got)
    75  		}
    76  	}
    77  }