| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System.Collections.Generic;
- using NUnit.Framework;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- namespace QuestPDF.UnitTests
- {
- [TestFixture]
- public class TextStyleTests
- {
- #region Font Weight
-
- [Test]
- public void FontWeight_Default()
- {
- var textStyle = TextStyle.LibraryDefault;
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
- }
-
- [Test]
- public void SetsCorrectSetsCorrectFontWeight_Thin()
- {
- var textStyle = TextStyle.Default.Thin();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Thin));
- }
-
- [Test]
- public void SetsCorrectFontWeight_ExtraLight()
- {
- var textStyle = TextStyle.Default.ExtraLight();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraLight));
- }
-
- [Test]
- public void SetsCorrectFontWeight_Light()
- {
- var textStyle = TextStyle.Default.Light();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Light));
- }
-
- [Test]
- public void SetsCorrectFontWeight_Normal()
- {
- var textStyle = TextStyle.Default.Bold().NormalWeight(); // first change from default, then normal
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
- }
-
- [Test]
- public void SetsCorrectFontWeight_Medium()
- {
- var textStyle = TextStyle.Default.Medium();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Medium));
- }
-
- [Test]
- public void SetsCorrectFontWeight_SemiBold()
- {
- var textStyle = TextStyle.Default.SemiBold();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.SemiBold));
- }
-
- [Test]
- public void SetsCorrectFontWeight_Bold()
- {
- var textStyle = TextStyle.Default.Bold();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Bold));
- }
-
- [Test]
- public void SetsCorrectFontWeight_ExtraBold()
- {
- var textStyle = TextStyle.Default.ExtraBold();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBold));
- }
-
- [Test]
- public void SetsCorrectFontWeight_Black()
- {
- var textStyle = TextStyle.Default.Black();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Black));
- }
-
- [Test]
- public void SetsCorrectFontWeight_ExtraBlack()
- {
- var textStyle = TextStyle.Default.ExtraBlack();
- Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBlack));
- }
-
- #endregion
-
- #region Text Position
-
- [Test]
- public void TextPosition_Default()
- {
- var textStyle = TextStyle.LibraryDefault;
- Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
- }
-
- [Test]
- public void SetsCorrectTextPosition_Subscript()
- {
- var textStyle = TextStyle.Default.Subscript();
- Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Subscript));
- }
-
- [Test]
- public void SetsCorrectTextPosition_Normal()
- {
- var textStyle = TextStyle.Default.Subscript().NormalPosition(); // first change from default, then normal
- Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
- }
-
- [Test]
- public void SetsCorrectTextPosition_Superscript()
- {
- var textStyle = TextStyle.Default.Superscript();
- Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Superscript));
- }
-
- #endregion
-
- #region Font Features
- [Test]
- public void FontFeatures_Default()
- {
- var textStyle = TextStyle.LibraryDefault;
- Assert.That(textStyle.FontFeatures, Is.Empty);
- }
-
- [Test]
- public void EnableFontFeature_SingleFeature()
- {
- var textStyle = TextStyle.Default
- .EnableFontFeature(FontFeatures.StandardLigatures);
-
- Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
- Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, true)));
- }
-
- [Test]
- public void DisableFontFeature_SingleFeature()
- {
- var textStyle = TextStyle.Default
- .DisableFontFeature(FontFeatures.Kerning);
-
- Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
- Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.Kerning, false)));
- }
-
- // NOTE: font features applied further down the chain override those applied earlier, and will appear first in the list
-
- [Test]
- public void FontFeatures_MixedEnableDisable()
- {
- var textStyle = TextStyle.Default
- .EnableFontFeature(FontFeatures.StandardLigatures)
- .DisableFontFeature(FontFeatures.Kerning)
- .EnableFontFeature(FontFeatures.DiscretionaryLigatures);
-
- Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(3));
- Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.DiscretionaryLigatures, true)));
- Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
- Assert.That(textStyle.FontFeatures[2], Is.EqualTo((FontFeatures.StandardLigatures, true)));
- }
-
- [Test]
- public void FontFeatures_OverrideSameFeature()
- {
- var textStyle = TextStyle.Default
- .EnableFontFeature(FontFeatures.StandardLigatures)
- .DisableFontFeature(FontFeatures.Kerning)
- .DisableFontFeature(FontFeatures.Kerning)
- .DisableFontFeature(FontFeatures.StandardLigatures);
-
- Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(2));
- Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, false)));
- Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
- }
-
- #endregion
-
- [Test]
- public void ApplyInheritedAndGlobalStyle()
- {
- // arrange
- var defaultTextStyle = TextStyle
- .Default
- .FontSize(20)
- .FontFamily("Arial", "Microsoft YaHei")
- .BackgroundColor(Colors.Green.Lighten2)
- .EnableFontFeature(FontFeatures.StandardLigatures);
- var spanTextStyle = TextStyle
- .Default
- .FontFamily("Times New Roman", "Arial", "Calibri")
- .Bold()
- .Strikethrough()
- .BackgroundColor(Colors.Red.Lighten2)
- .DisableFontFeature(FontFeatures.StandardLigatures)
- .EnableFontFeature(FontFeatures.Kerning);
-
- // act
- var targetStyle = spanTextStyle.ApplyInheritedStyle(defaultTextStyle).ApplyGlobalStyle();
-
- // assert
- var expectedStyle = TextStyle.LibraryDefault with
- {
- Id = targetStyle.Id, // expect to break when adding new TextStyle properties, so use the real one
- Size = 20,
- FontFamilies = new[] { "Times New Roman", "Arial", "Calibri", "Microsoft YaHei", "Lato" },
- FontWeight = FontWeight.Bold,
- BackgroundColor = Colors.Red.Lighten2,
- FontFeatures = new[]
- {
- (FontFeatures.Kerning, true),
- (FontFeatures.StandardLigatures, false)
- },
- HasStrikethrough = true
- };
- Assert.That(targetStyle, Is.Not.Null);
- Assert.That(targetStyle.Id, Is.GreaterThan(1));
- Assert.That(targetStyle.ToString(), Is.EqualTo(expectedStyle.ToString()));
- }
- }
- }
|