github.com/goplus/igop@v0.25.0/embed_test.go (about)

     1  //go:build go1.16
     2  // +build go1.16
     3  
     4  /*
     5   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package igop_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/goplus/igop"
    26  	_ "github.com/goplus/igop/pkg/embed"
    27  )
    28  
    29  func TestEmbed(t *testing.T) {
    30  	_, err := igop.Run("./testdata/embed", nil, 0)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  }
    35  
    36  func TestEmbedErrorNoMatching(t *testing.T) {
    37  	src := `package main
    38  
    39  import (
    40  	_ "embed"
    41  )
    42  
    43  //go:embed testdata/embed/testdata/data.txt
    44  var data string
    45  
    46  func main() {
    47  }
    48  `
    49  	_, err := igop.RunFile("main.go", src, nil, 0)
    50  	if err == nil {
    51  		t.Fatal("must panic")
    52  	}
    53  	t.Log(err)
    54  }
    55  
    56  func TestEmbedErrorMultipleVars(t *testing.T) {
    57  	src := `package main
    58  
    59  import (
    60  	_ "embed"
    61  )
    62  
    63  //go:embed testdata/embed/testdata/data1.txt
    64  var data1, data2 string
    65  
    66  func main() {
    67  }
    68  `
    69  	_, err := igop.RunFile("main.go", src, nil, 0)
    70  	if err == nil {
    71  		t.Fatal("must panic")
    72  	}
    73  	t.Log(err)
    74  }
    75  
    76  func TestEmbedErrorMisplaced(t *testing.T) {
    77  	src := `package main
    78  
    79  import (
    80  	_ "embed"
    81  )
    82  
    83  //go:embed testdata/embed/testdata/data1
    84  //var data1 string
    85  
    86  func main() {
    87  }
    88  `
    89  	_, err := igop.RunFile("main.go", src, nil, 0)
    90  	if err == nil {
    91  		t.Fatal("must panic")
    92  	}
    93  	t.Log(err)
    94  }
    95  
    96  func TestEmbedErrorCannotApply(t *testing.T) {
    97  	src := `package main
    98  
    99  import (
   100  	_ "embed"
   101  )
   102  
   103  //go:embed testdata/embed/testdata/data1.txt
   104  var data1 [][]byte
   105  
   106  func main() {
   107  }
   108  `
   109  	_, err := igop.RunFile("main.go", src, nil, 0)
   110  	if err == nil {
   111  		t.Fatal("must panic")
   112  	}
   113  	t.Log(err)
   114  }
   115  
   116  func TestEmbedErrorVarWithInitializer(t *testing.T) {
   117  	src := `package main
   118  
   119  import (
   120  	_ "embed"
   121  )
   122  
   123  //go:embed testdata/embed/testdata/data1.txt
   124  var data1 = "hello"
   125  
   126  func main() {
   127  }
   128  `
   129  	_, err := igop.RunFile("main.go", src, nil, 0)
   130  	if err == nil {
   131  		t.Fatal("must panic")
   132  	}
   133  	t.Log(err)
   134  }
   135  
   136  func TestEmbedErrorMultipleFiles(t *testing.T) {
   137  	src := `package main
   138  
   139  import (
   140  	_ "embed"
   141  )
   142  
   143  //go:embed testdata/embed/testdata
   144  var data1 []byte
   145  
   146  func main() {
   147  }
   148  `
   149  	_, err := igop.RunFile("main.go", src, nil, 0)
   150  	if err == nil {
   151  		t.Fatal("must panic")
   152  	}
   153  	t.Log(err)
   154  }