jar_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jar
  15. import (
  16. "bytes"
  17. "io"
  18. "testing"
  19. )
  20. func TestGetJavaPackage(t *testing.T) {
  21. type args struct {
  22. r io.Reader
  23. src string
  24. }
  25. tests := []struct {
  26. name string
  27. in string
  28. want string
  29. wantErr bool
  30. }{
  31. {
  32. name: "simple",
  33. in: "package foo.bar;",
  34. want: "foo.bar",
  35. },
  36. {
  37. name: "comment",
  38. in: "/* test */\npackage foo.bar;",
  39. want: "foo.bar",
  40. },
  41. {
  42. name: "no package",
  43. in: "import foo.bar;",
  44. want: "",
  45. },
  46. {
  47. name: "missing semicolon error",
  48. in: "package foo.bar",
  49. wantErr: true,
  50. },
  51. {
  52. name: "parser error",
  53. in: "/*",
  54. wantErr: true,
  55. },
  56. {
  57. name: "parser ident error",
  58. in: "package 0foo.bar;",
  59. wantErr: true,
  60. },
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. buf := bytes.NewBufferString(tt.in)
  65. got, err := JavaPackage(buf, "<test>")
  66. if (err != nil) != tt.wantErr {
  67. t.Errorf("JavaPackage() error = %v, wantErr %v", err, tt.wantErr)
  68. return
  69. }
  70. if got != tt.want {
  71. t.Errorf("JavaPackage() = %v, want %v", got, tt.want)
  72. }
  73. })
  74. }
  75. }
  76. func Test_javaIdentRune(t *testing.T) {
  77. // runes that should be valid anywhere in an identifier
  78. validAnywhere := []rune{
  79. // letters, $, _
  80. 'a',
  81. 'A',
  82. '$',
  83. '_',
  84. // assorted unicode
  85. '𐐀',
  86. '𐐨',
  87. 'Dž',
  88. 'ῼ',
  89. 'ʰ',
  90. '゚',
  91. 'ƻ',
  92. '㡢',
  93. '₩',
  94. '_',
  95. 'Ⅰ',
  96. '𐍊',
  97. }
  98. // runes that should be invalid as the first rune in an identifier, but valid anywhere else
  99. validAfterFirst := []rune{
  100. // digits
  101. '0',
  102. // assorted unicode
  103. '᥍',
  104. '𝟎',
  105. 'ྂ',
  106. '𝆀',
  107. // control characters
  108. '\x00',
  109. '\b',
  110. '\u000e',
  111. '\u001b',
  112. '\u007f',
  113. '\u009f',
  114. '\u00ad',
  115. 0xE007F,
  116. // zero width space
  117. '\u200b',
  118. }
  119. // runes that should never be valid in an identifier
  120. invalid := []rune{
  121. ';',
  122. 0x110000,
  123. }
  124. validFirst := validAnywhere
  125. invalidFirst := append(validAfterFirst, invalid...)
  126. validPart := append(validAnywhere, validAfterFirst...)
  127. invalidPart := invalid
  128. check := func(t *testing.T, ch rune, i int, want bool) {
  129. t.Helper()
  130. if got := javaIdentRune(ch, i); got != want {
  131. t.Errorf("javaIdentRune() = %v, want %v", got, want)
  132. }
  133. }
  134. t.Run("first", func(t *testing.T) {
  135. t.Run("valid", func(t *testing.T) {
  136. for _, ch := range validFirst {
  137. t.Run(string(ch), func(t *testing.T) {
  138. check(t, ch, 0, true)
  139. })
  140. }
  141. })
  142. t.Run("invalid", func(t *testing.T) {
  143. for _, ch := range invalidFirst {
  144. t.Run(string(ch), func(t *testing.T) {
  145. check(t, ch, 0, false)
  146. })
  147. }
  148. })
  149. })
  150. t.Run("part", func(t *testing.T) {
  151. t.Run("valid", func(t *testing.T) {
  152. for _, ch := range validPart {
  153. t.Run(string(ch), func(t *testing.T) {
  154. check(t, ch, 1, true)
  155. })
  156. }
  157. })
  158. t.Run("invalid", func(t *testing.T) {
  159. for _, ch := range invalidPart {
  160. t.Run(string(ch), func(t *testing.T) {
  161. check(t, ch, 1, false)
  162. })
  163. }
  164. })
  165. })
  166. }