ValidateCodeTests.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using Masuit.Tools.Strings;
  3. using Xunit;
  4. namespace Masuit.Tools.Abstractions.Test.Strings;
  5. public class ValidateCodeTests
  6. {
  7. [Fact]
  8. public void CreateValidateCode_ShouldReturnStringOfSpecifiedLength()
  9. {
  10. // Arrange
  11. int length = 6;
  12. // Act
  13. string result = ValidateCode.CreateValidateCode(length);
  14. // Assert
  15. Assert.NotNull(result);
  16. Assert.Equal(length, result.Length);
  17. }
  18. [Fact]
  19. public void CreateValidateGraphic_ShouldReturnPooledMemoryStream()
  20. {
  21. // Arrange
  22. string validateCode = "ABC123";
  23. int fontSize = 28;
  24. // Act
  25. using var result = validateCode.CreateValidateGraphic(fontSize);
  26. // Assert
  27. Assert.NotNull(result);
  28. Assert.True(result.Length > 0);
  29. }
  30. [Fact]
  31. public void StringWidth_ShouldReturnCorrectWidth()
  32. {
  33. // Arrange
  34. string input = "Test";
  35. int fontSize = 12;
  36. // Act
  37. float width = input.StringWidth(fontSize);
  38. // Assert
  39. Assert.True(width > 0);
  40. }
  41. [Fact]
  42. public void StringWidth_WithFontName_ShouldReturnCorrectWidth()
  43. {
  44. // Arrange
  45. string input = "Test";
  46. string fontName = "Microsoft YaHei UI";
  47. int fontSize = 12;
  48. // Act
  49. float width = input.StringWidth(fontName, fontSize);
  50. // Assert
  51. Assert.True(width > 0);
  52. }
  53. [Fact]
  54. public void StringWidth_WithInvalidFontName_ShouldThrowArgumentException()
  55. {
  56. // Arrange
  57. string input = "Test";
  58. string fontName = "InvalidFont";
  59. int fontSize = 12;
  60. // Act & Assert
  61. var exception = Assert.Throws<ArgumentException>(() => input.StringWidth(fontName, fontSize));
  62. Assert.Equal($"字体 {fontName} 不存在,请尝试其它字体!", exception.Message);
  63. }
  64. }