completion_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2016 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."},
  40. },
  41. {
  42. input: "x.someMethod",
  43. want: []string{"x.someMethod("},
  44. },
  45. {
  46. input: "x.",
  47. want: []string{
  48. "x.constructor",
  49. "x.foo",
  50. "x.gazonk",
  51. "x.someMethod",
  52. },
  53. },
  54. {
  55. input: "y.",
  56. want: []string{
  57. "y.constructor",
  58. "y.foo",
  59. "y.gazonk",
  60. "y.someMethod",
  61. },
  62. },
  63. {
  64. input: "x.gazonk.",
  65. want: []string{
  66. "x.gazonk.constructor",
  67. "x.gazonk.hasOwnProperty",
  68. "x.gazonk.isPrototypeOf",
  69. "x.gazonk.propertyIsEnumerable",
  70. "x.gazonk.toLocaleString",
  71. "x.gazonk.toString",
  72. "x.gazonk.valueOf",
  73. "x.gazonk.xyz",
  74. },
  75. },
  76. }
  77. for _, test := range tests {
  78. cs := re.CompleteKeywords(test.input)
  79. if !reflect.DeepEqual(cs, test.want) {
  80. t.Errorf("wrong completions for %q\ngot %v\nwant %v", test.input, cs, test.want)
  81. }
  82. }
  83. }