Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. // Command precisTests opens test/golden.json from the python version of Precis
  2. // and attempts to run them against the Go version.
  3. //
  4. // Right now Python 3.7.4 appears to use Unicode Version 11.
  5. // This means this program should be run with Go 1.13 to use the same version.
  6. // This is gotip as of this gist.
  7. //
  8. // To install gotip try something like this:
  9. //
  10. // go get golang.org/dl/gotip
  11. // gotip download
  12. // gotip run precisTests.go
  13. package main
  14.  
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "log"
  20. "os"
  21.  
  22. "golang.org/x/text/secure/precis"
  23. "golang.org/x/text/unicode/norm"
  24. )
  25.  
  26. type testCase struct {
  27. Profile string `json:"profile"`
  28. Input string `json:"input"`
  29. Output string `json:"output"`
  30. Error string `json:"error"`
  31. }
  32.  
  33. func main() {
  34. f, err := os.Open("test/golden.json")
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. defer f.Close()
  39.  
  40. d := json.NewDecoder(f)
  41. // Pop the open slice token so we can stream the elements instead of buffering
  42. // them all into memory.
  43. _, err = d.Token()
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. for i := 0; d.More(); i++ {
  48. var tc testCase
  49. switch err := d.Decode(&tc); err {
  50. case io.EOF:
  51. return
  52. case nil:
  53. default:
  54. log.Fatal(err)
  55. }
  56.  
  57. err = runTest(tc)
  58. if err != nil {
  59. log.Printf("Error in test %d: %v", i, err)
  60. }
  61. }
  62. }
  63.  
  64. func runTest(tc testCase) error {
  65. var p *precis.Profile
  66. switch tc.Profile {
  67. case "Nickname":
  68. p = precis.Nickname
  69. case "NicknameCasePreserved":
  70. // TODO: one of these tests triggers a bug in the precis library that I need
  71. // to track down. Disable until such a time as I can fix it.
  72. return nil
  73. case "UsernameCaseMapped", "UsernameCaseMapped:ToLower":
  74. p = precis.UsernameCaseMapped
  75. case "UsernameCaseMapped:CaseFold":
  76. // This matches the definition of precis.UsernameCaseMapped except with
  77. // ToLower changed back to FoldCase like in previous versions of the precis
  78. // library.
  79. p = precis.NewIdentifier(
  80. precis.FoldWidth,
  81. precis.FoldCase(),
  82. precis.Norm(norm.NFC),
  83. precis.BidiRule,
  84. )
  85. case "UsernameCasePreserved":
  86. p = precis.UsernameCasePreserved
  87. case "OpaqueString":
  88. p = precis.OpaqueString
  89. case "IdentifierClass", "FreeFormClass", "NicknameCaseMapped:ToLower",
  90. "NicknameCaseMapped:CaseFold", "NicknameCaseMapped":
  91. // TODO: figure out exactly what the Python library is doing with each of
  92. // these non-standard profiles and create a new profile that mimics their
  93. // behavior.
  94. return nil
  95. default:
  96. return fmt.Errorf("unexpected profile %q", tc.Profile)
  97. }
  98.  
  99. out, err := p.String(tc.Input)
  100. // This error handling doesn't handle the case where we get an error that's
  101. // different than what's expected, but that will require a lot more
  102. // boilerplate.
  103. switch {
  104. case err == nil && tc.Error != "":
  105. return fmt.Errorf("Did not get expected error %q", tc.Error)
  106. case err != nil && tc.Error == "":
  107. return fmt.Errorf("Unexpected error: %v", err)
  108. }
  109.  
  110. if out != tc.Output {
  111. return fmt.Errorf("Unexpected output when applying %s to %q (%0 [2]x): want=%q (%0 [3]x), got=%q (%0 [4]x)", tc.Profile, tc.Input, tc.Output, out)
  112. }
  113. return nil
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement