utils_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import "testing"
  3. func TestCanInt(t *testing.T) {
  4. if !CanInt("12345") {
  5. t.Errorf("Expected valid int conversion of '12345'")
  6. }
  7. if CanInt("9.81") {
  8. t.Errorf("Unexpected int conversion of '9.81', this should return true on CanFloat (not CanInt)")
  9. }
  10. if !CanInt("-42") {
  11. t.Errorf("Expected valid int conversion of '-42'")
  12. }
  13. if CanInt("-24-88") {
  14. t.Errorf("Unexpected int conversion of '-24-88', this should not be allowed as we have 2 '-'")
  15. }
  16. if !CanInt("-1234567890") {
  17. t.Errorf("Expected valid int conversion of '-1234567890'")
  18. }
  19. if CanInt("123abc") {
  20. t.Errorf("Unexpected int conversion of '123abc', contains letters 'abc' which are not numerics")
  21. }
  22. }
  23. func TestCanFloat(t *testing.T) {
  24. if !CanFloat("9.81") {
  25. t.Errorf("Expected valid float conversion of '9.81'")
  26. }
  27. if CanFloat("198.164.255.0") {
  28. t.Errorf("Unexpected float conversion of '198.164.255.0', this is a IP, not a floating value")
  29. }
  30. if !CanFloat("-42.0") {
  31. t.Errorf("Expected valid float conversion of '-42.0'")
  32. }
  33. if !CanFloat("1234567890.1234567890") {
  34. t.Errorf("Expected valid float conversion of '1234567890.1234567890'")
  35. }
  36. if CanFloat("-123.-1234") {
  37. t.Errorf("Unexpected float conversion of '-123.-1234', contains 2 '-'")
  38. }
  39. if !CanFloat("0.15") {
  40. t.Errorf("Expected valid float conversion of '0.15'")
  41. }
  42. if !CanFloat(".15") {
  43. t.Errorf("Expected valid float conversion of '.15'")
  44. }
  45. if CanFloat("-123.abc") {
  46. t.Errorf("Unexpected float conversion of '-123.abc', contains 'abc' which are not numerics")
  47. }
  48. }
  49. func TestFormatCredits(t *testing.T) {
  50. if FormatCredit(1000) != "1,000" {
  51. t.Errorf("Expected '1,000' from 1000, got '%s'", FormatCredit(1000))
  52. }
  53. if FormatCredit(10000) != "10,000" {
  54. t.Errorf("Expected '10,000' from 10000, got '%s'", FormatCredit(10000))
  55. }
  56. if FormatCredit(1000000) != "1,000,000" {
  57. t.Errorf("Expected '1,000,000' from 1000000, got '%s'", FormatCredit(1000000))
  58. }
  59. if FormatCredit(-4000) != "-4,000" {
  60. t.Errorf("Expected '-4,000' from -4000, got '%s'", FormatCredit(-4000))
  61. }
  62. }