completion_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package jsre
  17. import (
  18. "reflect"
  19. "testing"
  20. )
  21. func TestCompleteKeywords(t *testing.T) {
  22. re := New("")
  23. re.Run(`
  24. function theClass() {
  25. this.foo = 3;
  26. this.gazonk = {xyz: 4};
  27. }
  28. theClass.prototype.someMethod = function () {};
  29. var x = new theClass();
  30. var y = new theClass();
  31. y.someMethod = function override() {};
  32. `)
  33. var tests = []struct {
  34. input string
  35. want []string
  36. }{
  37. {
  38. input: "x",
  39. want: []string{"x", "x."},
  40. },
  41. {
  42. input: "x.",
  43. want: []string{
  44. "x.constructor",
  45. "x.foo",
  46. "x.gazonk",
  47. "x.someMethod",
  48. },
  49. },
  50. {
  51. input: "y.",
  52. want: []string{
  53. "y.constructor",
  54. "y.foo",
  55. "y.gazonk",
  56. "y.someMethod",
  57. },
  58. },
  59. {
  60. input: "x.gazonk.",
  61. want: []string{
  62. "x.gazonk.constructor",
  63. "x.gazonk.hasOwnProperty",
  64. "x.gazonk.isPrototypeOf",
  65. "x.gazonk.propertyIsEnumerable",
  66. "x.gazonk.toLocaleString",
  67. "x.gazonk.toString",
  68. "x.gazonk.valueOf",
  69. "x.gazonk.xyz",
  70. },
  71. },
  72. }
  73. for _, test := range tests {
  74. cs := re.CompleteKeywords(test.input)
  75. if !reflect.DeepEqual(cs, test.want) {
  76. t.Errorf("wrong completions for %q\ngot %v\nwant %v", test.input, cs, test.want)
  77. }
  78. }
  79. }