// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

// Flags: --harmony-computed-property-names --harmony-classes


function ID(x) {
  return x;
}


(function TestClassMethodString() {
  class C {
    a() { return 'A'}
    ['b']() { return 'B'; }
    c() { return 'C'; }
    [ID('d')]() { return 'D'; }
  }
  assertEquals('A', new C().a());
  assertEquals('B', new C().b());
  assertEquals('C', new C().c());
  assertEquals('D', new C().d());
  assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C.prototype));
})();


(function TestClassMethodNumber() {
  class C {
    a() { return 'A'; }
    [1]() { return 'B'; }
    c() { return 'C'; }
    [ID(2)]() { return 'D'; }
  }
  assertEquals('A', new C().a());
  assertEquals('B', new C()[1]());
  assertEquals('C', new C().c());
  assertEquals('D', new C()[2]());
  // Array indexes first.
  assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C.prototype));
})();


(function TestClassMethodSymbol() {
  var sym1 = Symbol();
  var sym2 = Symbol();
  class C {
    a() { return 'A'; }
    [sym1]() { return 'B'; }
    c() { return 'C'; }
    [ID(sym2)]() { return 'D'; }
  }
  assertEquals('A', new C().a());
  assertEquals('B', new C()[sym1]());
  assertEquals('C', new C().c());
  assertEquals('D', new C()[sym2]());
  assertArrayEquals(['a', 'c'], Object.keys(C.prototype));
  assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C.prototype));
})();



(function TestStaticClassMethodString() {
  class C {
    static a() { return 'A'}
    static ['b']() { return 'B'; }
    static c() { return 'C'; }
    static ['d']() { return 'D'; }
  }
  assertEquals('A', C.a());
  assertEquals('B', C.b());
  assertEquals('C', C.c());
  assertEquals('D', C.d());
  assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C));
})();


(function TestStaticClassMethodNumber() {
  class C {
    static a() { return 'A'; }
    static [1]() { return 'B'; }
    static c() { return 'C'; }
    static [2]() { return 'D'; }
  }
  assertEquals('A', C.a());
  assertEquals('B', C[1]());
  assertEquals('C', C.c());
  assertEquals('D', C[2]());
  // Array indexes first.
  assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C));
})();


(function TestStaticClassMethodSymbol() {
  var sym1 = Symbol();
  var sym2 = Symbol();
  class C {
    static a() { return 'A'; }
    static [sym1]() { return 'B'; }
    static c() { return 'C'; }
    static [sym2]() { return 'D'; }
  }
  assertEquals('A', C.a());
  assertEquals('B', C[sym1]());
  assertEquals('C', C.c());
  assertEquals('D', C[sym2]());
  assertArrayEquals(['a', 'c'], Object.keys(C));
  assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C));
})();



function assertIteratorResult(value, done, result) {
  assertEquals({ value: value, done: done}, result);
}


(function TestGeneratorComputedName() {
  class C {
    *['a']() {
      yield 1;
      yield 2;
    }
  }
  var iter = new C().a();
  assertIteratorResult(1, false, iter.next());
  assertIteratorResult(2, false, iter.next());
  assertIteratorResult(undefined, true, iter.next());
  assertArrayEquals(['a'], Object.keys(C.prototype));
})();


(function TestToNameSideEffects() {
  var counter = 0;
  var key1 = {
    toString: function() {
      assertEquals(0, counter++);
      return 'b';
    }
  };
  var key2 = {
    toString: function() {
      assertEquals(1, counter++);
      return 'd';
    }
  };
  class C {
    a() { return 'A'; }
    [key1]() { return 'B'; }
    c() { return 'C'; }
    [key2]() { return 'D'; }
  }
  assertEquals(2, counter);
  assertEquals('A', new C().a());
  assertEquals('B', new C().b());
  assertEquals('C', new C().c());
  assertEquals('D', new C().d());
  assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(C.prototype));
})();


(function TestToNameSideEffectsNumbers() {
  var counter = 0;
  var key1 = {
    valueOf: function() {
      assertEquals(0, counter++);
      return 1;
    },
    toString: null
  };
  var key2 = {
    valueOf: function() {
      assertEquals(1, counter++);
      return 2;
    },
    toString: null
  };

  class C {
    a() { return 'A'; }
    [key1]() { return 'B'; }
    c() { return 'C'; }
    [key2]() { return 'D'; }
  }
  assertEquals(2, counter);
  assertEquals('A', new C().a());
  assertEquals('B', new C()[1]());
  assertEquals('C', new C().c());
  assertEquals('D', new C()[2]());
  // Array indexes first.
  assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(C.prototype));
})();


(function TestGetter() {
  class C {
    get ['a']() {
      return 'A';
    }
  }
  assertEquals('A', new C().a);

  class C2 {
    get b() {
      assertUnreachable();
    }
    get ['b']() {
      return 'B';
    }
  }
  assertEquals('B', new C2().b);

  class C3 {
    get c() {
      assertUnreachable();
    }
    get ['c']() {
      assertUnreachable();
    }
    get ['c']() {
      return 'C';
    }
  }
  assertEquals('C', new C3().c);

  class C4 {
    get ['d']() {
      assertUnreachable();
    }
    get d() {
      return 'D';
    }
  }
  assertEquals('D', new C4().d);
})();


(function TestSetter() {
  var calls = 0;
  class C {
    set ['a'](_) {
      calls++;
    }
  }
  new C().a = 'A';
  assertEquals(1, calls);

  calls = 0;
  class C2 {
    set b(_) {
      assertUnreachable();
    }
    set ['b'](_) {
      calls++;
    }
  }
  new C2().b = 'B';
  assertEquals(1, calls);

  calls = 0;
  class C3 {
    set c(_) {
      assertUnreachable()
    }
    set ['c'](_) {
      assertUnreachable()
    }
    set ['c'](_) {
      calls++
    }
  }
  new C3().c = 'C';
  assertEquals(1, calls);

  calls = 0;
  class C4 {
    set ['d'](_) {
      assertUnreachable()
    }
    set d(_) {
      calls++
    }
  }
  new C4().d = 'D';
  assertEquals(1, calls);
})();


(function TestPrototype() {
  // Normally a static prototype property is not allowed.
  class C {
    static ['prototype']() {
      return 1;
    }
  }
  assertEquals(1, C.prototype());

  class C2 {
    static get ['prototype']() {
      return 2;
    }
  }
  assertEquals(2, C2.prototype);

  var calls = 0;
  class C3 {
    static set ['prototype'](x) {
      assertEquals(3, x);
      calls++;
    }
  }
  C3.prototype = 3;
  assertEquals(1, calls);

  class C4 {
    static *['prototype']() {
      yield 1;
      yield 2;
    }
  }
  var iter = C4.prototype();
  assertIteratorResult(1, false, iter.next());
  assertIteratorResult(2, false, iter.next());
  assertIteratorResult(undefined, true, iter.next());
})();


(function TestConstructor() {
  // Normally a constructor property is not allowed.
  class C {
    ['constructor']() {
      return 1;
    }
  }
  assertTrue(C !== C.prototype.constructor);
  assertEquals(1, new C().constructor());

  class C2 {
    get ['constructor']() {
      return 2;
    }
  }
  assertEquals(2, new C2().constructor);

  var calls = 0;
  class C3 {
    set ['constructor'](x) {
      assertEquals(3, x);
      calls++;
    }
  }
  new C3().constructor = 3;
  assertEquals(1, calls);

  class C4 {
    *['constructor']() {
      yield 1;
      yield 2;
    }
  }
  var iter = new C4().constructor();
  assertIteratorResult(1, false, iter.next());
  assertIteratorResult(2, false, iter.next());
  assertIteratorResult(undefined, true, iter.next());
})();
