github.com/nuvolaris/goja@v0.0.0-20230825100449-967811910c6d/builtin_set_test.go (about)

     1  package goja
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestSetEvilIterator(t *testing.T) {
    10  	const SCRIPT = `
    11  	var o = {};
    12  	o[Symbol.iterator] = function() {
    13  		return {
    14  			next: function() {
    15  				if (!this.flag) {
    16  					this.flag = true;
    17  					return {};
    18  				}
    19  				return {done: true};
    20  			}
    21  		}
    22  	}
    23  	new Set(o);
    24  	undefined;
    25  	`
    26  	testScript(SCRIPT, _undefined, t)
    27  }
    28  
    29  func ExampleRuntime_ExportTo_setToMap() {
    30  	vm := New()
    31  	s, err := vm.RunString(`
    32  	new Set([1, 2, 3])
    33  	`)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	m := make(map[int]struct{})
    38  	err = vm.ExportTo(s, &m)
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  	fmt.Println(m)
    43  	// Output: map[1:{} 2:{} 3:{}]
    44  }
    45  
    46  func ExampleRuntime_ExportTo_setToSlice() {
    47  	vm := New()
    48  	s, err := vm.RunString(`
    49  	new Set([1, 2, 3])
    50  	`)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	var a []int
    55  	err = vm.ExportTo(s, &a)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  	fmt.Println(a)
    60  	// Output: [1 2 3]
    61  }
    62  
    63  func TestSetExportToSliceCircular(t *testing.T) {
    64  	vm := New()
    65  	s, err := vm.RunString(`
    66  	let s = new Set();
    67  	s.add(s);
    68  	s;
    69  	`)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	var a []Value
    74  	err = vm.ExportTo(s, &a)
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	if len(a) != 1 {
    79  		t.Fatalf("len: %d", len(a))
    80  	}
    81  	if a[0] != s {
    82  		t.Fatalf("a: %v", a)
    83  	}
    84  }
    85  
    86  func TestSetExportToArrayMismatchedLengths(t *testing.T) {
    87  	vm := New()
    88  	s, err := vm.RunString(`
    89  	new Set([1, 2])
    90  	`)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	var s1 [3]int
    95  	err = vm.ExportTo(s, &s1)
    96  	if err == nil {
    97  		t.Fatal("expected error")
    98  	}
    99  	if msg := err.Error(); !strings.Contains(msg, "lengths mismatch") {
   100  		t.Fatalf("unexpected error: %v", err)
   101  	}
   102  }
   103  
   104  func TestSetExportToNilMap(t *testing.T) {
   105  	vm := New()
   106  	var m map[int]interface{}
   107  	res, err := vm.RunString("new Set([1])")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	err = vm.ExportTo(res, &m)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	if len(m) != 1 {
   116  		t.Fatal(m)
   117  	}
   118  	if _, exists := m[1]; !exists {
   119  		t.Fatal(m)
   120  	}
   121  }
   122  
   123  func TestSetExportToNonNilMap(t *testing.T) {
   124  	vm := New()
   125  	m := map[int]interface{}{
   126  		2: true,
   127  	}
   128  	res, err := vm.RunString("new Set([1])")
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	err = vm.ExportTo(res, &m)
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  	if len(m) != 1 {
   137  		t.Fatal(m)
   138  	}
   139  	if _, exists := m[1]; !exists {
   140  		t.Fatal(m)
   141  	}
   142  }
   143  
   144  func TestSetGetAdderGetIteratorOrder(t *testing.T) {
   145  	const SCRIPT = `
   146  	let getterCalled = 0;
   147  
   148  	class S extends Set {
   149  	    get add() {
   150  	        getterCalled++;
   151  	        return null;
   152  	    }
   153  	}
   154  
   155  	let getIteratorCalled = 0;
   156  
   157  	let iterable = {};
   158  	iterable[Symbol.iterator] = () => {
   159  	    getIteratorCalled++
   160  	    return {
   161  	        next: 1
   162  	    };
   163  	}
   164  
   165  	let thrown = false;
   166  
   167  	try {
   168  	    new S(iterable);
   169  	} catch (e) {
   170  	    if (e instanceof TypeError) {
   171  	        thrown = true;
   172  	    } else {
   173  	        throw e;
   174  	    }
   175  	}
   176  
   177  	thrown && getterCalled === 1 && getIteratorCalled === 0;
   178  	`
   179  	testScript(SCRIPT, valueTrue, t)
   180  }