github.com/openconfig/goyang@v1.4.5/pkg/yangentry/build_yang_test.go (about)

     1  // Copyright 2020 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package yangentry
    16  
    17  import "testing"
    18  
    19  // TestParse tests the Parse function - which takes an input
    20  // set of modules and processes them using the goyang compiler into a set of
    21  // yang.Entry pointers.
    22  func TestParse(t *testing.T) {
    23  	tests := []struct {
    24  		name     string
    25  		inFiles  []string
    26  		inPath   []string
    27  		wantErr  bool
    28  		wantMods []string
    29  	}{{
    30  		name:     "simple valid module",
    31  		inFiles:  []string{"testdata/00-valid-module.yang"},
    32  		inPath:   []string{"testdata"},
    33  		wantMods: []string{"test-module"},
    34  	}, {
    35  		name:     "simple valid module without .yang extension",
    36  		inFiles:  []string{"00-valid-module"},
    37  		inPath:   []string{"testdata"},
    38  		wantMods: []string{"test-module"},
    39  	}, {
    40  		name:    "simple invalid module",
    41  		inFiles: []string{"testdata/01-invalid-module.yang"},
    42  		inPath:  []string{"testdata"},
    43  		wantErr: true,
    44  	}, {
    45  		name:     "valid import",
    46  		inFiles:  []string{"testdata/02-valid-import.yang"},
    47  		inPath:   []string{"testdata/subdir"},
    48  		wantMods: []string{"test-module"},
    49  	}, {
    50  		name:    "invalid import",
    51  		inFiles: []string{"testdata/03-invalid-import.yang"},
    52  		inPath:  []string{},
    53  		wantErr: true,
    54  	}, {
    55  		name:     "two modules",
    56  		inFiles:  []string{"testdata/04-valid-module-one.yang", "testdata/04-valid-module-two.yang"},
    57  		inPath:   []string{},
    58  		wantMods: []string{"module-one", "module-two"},
    59  	}}
    60  
    61  	for _, tt := range tests {
    62  		entries, errs := Parse(tt.inFiles, tt.inPath)
    63  		if len(errs) != 0 && !tt.wantErr {
    64  			t.Errorf("%s: unexpected error processing modules: %v", tt.name, errs)
    65  			continue
    66  		}
    67  
    68  		for _, m := range tt.wantMods {
    69  			if _, ok := entries[m]; !ok {
    70  				t.Errorf("%s: could not find module %s", tt.name, m)
    71  			}
    72  		}
    73  	}
    74  }