github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/compiler/desugarer/desugarer_test.go (about)

     1  package desugarer
     2  
     3  import (
     4  	"testing"
     5  
     6  	src "github.com/nevalang/neva/internal/compiler/sourcecode"
     7  	"github.com/nevalang/neva/pkg"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // TODO
    12  // 1) refactor this test, split into sub-method tests
    13  // 2) Write test for deferred connection and make sure it works
    14  // 3) Fix deferred connection if needed
    15  // 4) Find problem with program number 4 with_bridge if needed
    16  
    17  func TestDesugarer_desugarModule(t *testing.T) {
    18  	tests := []struct {
    19  		name    string
    20  		mod     src.Module
    21  		want    src.Module
    22  		wantErr bool
    23  	}{
    24  		// every output module must have std module dependency
    25  		{
    26  			name: "every desugared module has std mod dep with right version",
    27  			mod: src.Module{
    28  				Manifest: src.ModuleManifest{
    29  					Deps: map[string]src.ModuleRef{}, // <-- no std mod dep
    30  				},
    31  				Packages: map[string]src.Package{},
    32  			},
    33  			want: src.Module{
    34  				Manifest: src.ModuleManifest{
    35  					Deps: defaultDeps, // <-- std mod dep
    36  				},
    37  				Packages: map[string]src.Package{},
    38  			},
    39  			wantErr: false,
    40  		},
    41  	}
    42  
    43  	d := Desugarer{}
    44  
    45  	for _, tt := range tests {
    46  		tt := tt
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			modRef := src.ModuleRef{Path: "@"}
    49  			build := src.Build{
    50  				Modules: map[src.ModuleRef]src.Module{
    51  					modRef: tt.mod,
    52  				},
    53  			}
    54  
    55  			got, err := d.desugarModule(build, modRef)
    56  			require.Equal(t, err != nil, tt.wantErr)
    57  			require.Equal(t, tt.want, got)
    58  		})
    59  	}
    60  }
    61  
    62  func TestDesugarer_desugarFile(t *testing.T) {
    63  	tests := []struct {
    64  		name    string
    65  		file    src.File
    66  		want    src.File
    67  		wantErr bool
    68  	}{
    69  		{
    70  			name: "every desugared file has std/builtin import",
    71  			file: src.File{
    72  				Imports: map[string]src.Import{}, // <-- no imports of std/builtin
    73  			},
    74  			want: src.File{
    75  				Imports:  defaultImports(), // <-- std/builtin import
    76  				Entities: map[string]src.Entity{},
    77  			},
    78  			wantErr: false,
    79  		},
    80  	}
    81  	d := Desugarer{}
    82  	for _, tt := range tests {
    83  		tt := tt
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			got, err := d.desugarFile(tt.file, src.Scope{})
    86  			require.Equal(t, err != nil, tt.wantErr)
    87  			require.Equal(t, tt.want, got)
    88  		})
    89  	}
    90  }
    91  
    92  // === Helpers ===
    93  
    94  func defaultImports() map[string]src.Import {
    95  	return map[string]src.Import{
    96  		"builtin": {
    97  			Module:  "std",
    98  			Package: "builtin",
    99  		},
   100  	}
   101  }
   102  
   103  var defaultDeps = map[string]src.ModuleRef{
   104  	"std": {
   105  		Path:    "std",
   106  		Version: pkg.Version,
   107  	},
   108  }