├── .gitattributes
├── .gitignore
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── CHANGELOG.md
├── CONTRIBUTING.md
├── CreateNuget.cmd
├── DeployToNuget.cmd
├── HowToRelease.txt
├── LICENSE.md
├── README.md
├── Settings.StyleCop
├── StatePrinter.Tests
├── Configurations
│ └── ConfigurationTest.cs
├── ExamplesForDocumentation
│ ├── ExampleEndlessAsserts.cs
│ ├── ExampleListAndArrays.cs
│ └── ExampleOnSimpleAsserts.cs
├── FieldHarvesters
│ ├── AllHarvesterTest.cs
│ ├── AnonymousHarvesterTest.cs
│ ├── ProjectionHarvesterByTypeTest.cs
│ ├── ProjectionHarvesterTest.cs
│ └── ToStringAwareHarvesterTest.cs
├── IntegrationTests
│ ├── CallStackReflectorTest.cs
│ ├── CultureTests.cs
│ ├── DictionaryTest.cs
│ ├── IEnumeratbleTest.cs
│ ├── InheritanceTest.cs
│ ├── ObjectGraphsTest.cs
│ ├── PropertiesTest.cs
│ ├── PublicPropertiesTest.cs
│ ├── StandardConfigurationTest.cs
│ ├── ToStringMethodTest.cs
│ └── TwoDimensionArrayTest.cs
├── Introspection
│ ├── ReferenceTest.cs
│ └── TokenTest.cs
├── Mocks
│ └── Mocks.cs
├── OutputFormatters
│ ├── RollingGuidValueConverterTest.cs
│ ├── StringBuilderTrimmerTest.cs
│ └── TokenFilterTest.cs
├── PerformanceTests
│ ├── ManySmallCollections.cs
│ ├── ManySmallObjects.cs
│ ├── PerformanceTestsBase.cs
│ └── ToStringTests.cs
├── Properties
│ └── AssemblyInfo.cs
├── StatePrinter.Tests.csproj
├── TestHelper.cs
├── TestingAssistance
│ ├── EnvironmentReaderTest.cs
│ ├── ParserTest.cs
│ ├── ReWriterMockedTests.cs
│ ├── TestingAssistanceReWriteTest.cs
│ ├── TestingAssistanceTest.cs
│ └── USerStory.cs
├── ValueConverters
│ └── GenericValueConverterTest.cs
└── packages.config
├── StatePrinter.nuspec
├── StatePrinter.sln
├── StatePrinter
├── Configurations
│ ├── Configuration.cs
│ ├── ConfigurationHelper.cs
│ ├── LegacyBehaviour.cs
│ └── TestingBehaviour.cs
├── FieldHarvesters
│ ├── AllFieldsAndPropertiesHarvester.cs
│ ├── AllFieldsHarvester.cs
│ ├── AnonymousFieldHarvester.cs
│ ├── HarvestHelper.cs
│ ├── IFieldHarvester.cs
│ ├── IRunTimeCodeGenerator.cs
│ ├── ProjectionHarvester.cs
│ ├── PublicFieldsAndPropertiesHarvester .cs
│ ├── PublicFieldsHarvester.cs
│ ├── RunTimeCodeGenerator.cs
│ ├── SanitizedFieldInfo.cs
│ └── ToStringAwareHarvester.cs
├── Introspection
│ ├── HarvestInfoCache.cs
│ ├── IntroSpector.cs
│ ├── Reference.cs
│ ├── ReflectionInfo.cs
│ ├── Token.cs
│ └── TokenType.cs
├── OutputFormatters
│ ├── CurlyBraceStyle.cs
│ ├── FastestPossibleStyle.cs
│ ├── IOutputFormatter.cs
│ ├── IndentingStringBuilder.cs
│ ├── JsonStyle.cs
│ ├── OutputFormatterHelpers.cs
│ ├── StringBuilderTrimmer.cs
│ ├── UnusedReferencesTokenFilter.cs
│ └── XmlStyle.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ └── Resources.resx
├── Settings.StyleCop
├── StatePrinter.cs
├── StatePrinter.csproj
├── StatePrinterStrongNameKeyFile_DoNotDelete.snk
├── TestAssistance
│ ├── Asserter.cs
│ ├── DefaultAssertMessage.cs
│ ├── EnvironmentReader.cs
│ ├── FileRepository.cs
│ ├── Parser.cs
│ ├── StringUtils.cs
│ └── TestRewriter.cs
├── ValueConverters
│ ├── DateTimeConverter.cs
│ ├── EnumConverter.cs
│ ├── GenericValueConverter.cs
│ ├── IValueConverter.cs
│ ├── RollingGuidValueConverter.cs
│ ├── StandardTypesConverter.cs
│ └── StringConverter.cs
└── gfx
│ ├── stateprinter.ico
│ └── stateprinter.png
├── appveyor.yml
└── doc
├── AutomatingToStrings.md
├── AutomatingUnitTesting.md
├── ComprehensibleTestingOfGuids.md
├── HowToConfigure.md
└── TheProblemsWithTraditionalUnitTesting.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Set the default behavior, in case people don't have core.autocrlf set.
2 | * eol=crlf
3 |
4 | # Denote all files that are truly binary and should not be modified.
5 | *.snk binary
6 | *.exe binary
7 |
8 | *.dll binary
9 | *.png binary
10 | *.jpg binary
11 | *.gif binary
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2 | [Bb]in/
3 | [Oo]bj/
4 |
5 | # mstest test results
6 | TestResults
7 |
8 | ## Ignore Visual Studio temporary files, build results, and
9 | ## files generated by popular Visual Studio add-ons.
10 |
11 | # User-specific files
12 | *.suo
13 | *.user
14 | *.sln.docstates
15 | .vs/
16 |
17 | # Build results
18 | [Dd]ebug/
19 | [Rr]elease/
20 | x64/
21 | *_i.c
22 | *_p.c
23 | *.ilk
24 | *.meta
25 | *.obj
26 | *.pch
27 | *.pdb
28 | *.pgc
29 | *.pgd
30 | *.rsp
31 | *.sbr
32 | *.tlb
33 | *.tli
34 | *.tlh
35 | *.tmp
36 | *.log
37 | *.vspscc
38 | *.vssscc
39 | .builds
40 |
41 |
42 | # Visual Studio profiler
43 | *.psess
44 | *.vsp
45 | *.vspx
46 |
47 | # Guidance Automation Toolkit
48 | *.gpState
49 |
50 | # ReSharper is a .NET coding add-in
51 | _ReSharper*
52 |
53 | # NCrunch
54 | *.ncrunch*
55 | .*crunch*.local.xml
56 | _NCrunch_*
57 |
58 | # Installshield output folder
59 | [Ee]xpress
60 |
61 | # DocProject is a documentation generator add-in
62 | DocProject/buildhelp/
63 | DocProject/Help/*.HxT
64 | DocProject/Help/*.HxC
65 | DocProject/Help/*.hhc
66 | DocProject/Help/*.hhk
67 | DocProject/Help/*.hhp
68 | DocProject/Help/Html2
69 | DocProject/Help/html
70 |
71 | # Click-Once directory
72 | publish
73 |
74 | # Publish Web Output
75 | *.Publish.xml
76 |
77 | # NuGet Packages Directory
78 | packages
79 | nuget_packages/
80 |
81 | # Windows Azure Build Output
82 | csx
83 | *.build.csdef
84 |
85 | # Windows Store app package directory
86 | AppPackages/
87 |
88 | # Others
89 | [Bb]in
90 | [Oo]bj
91 | sql
92 | TestResults
93 | [Tt]est[Rr]esult*
94 | *.Cache
95 | ClientBin
96 | [Ss]tyle[Cc]op.*
97 | ~$*
98 | *.dbmdl
99 | Generated_Code #added for RIA/Silverlight projects
100 |
101 | # Backup & report files from converting an old project file to a newer
102 | # Visual Studio version. Backup files are not needed, because we have git ;-)
103 | _UpgradeReport_Files/
104 | Backup*/
105 | UpgradeLog*.XML
106 | /.project
107 |
108 | *.orig
109 |
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kbilsted/StatePrinter/76e70caf955303a39116dbf4e750efb9fcea1972/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # Version history
4 |
5 | Full documentation on usage and motivating examples at https://github.com/kbilsted/StatePrinter/tree/master/doc
6 |
7 | ## V4.0 (unreleased)
8 | * Requires .NET Framework 4.7.2
9 | * Added `TestingBehaviour.SetAreEqualsMethod()` for easier integration with NUnit v3.x
10 | * Removed all obsolete functionality.
11 |
12 | ## 3.0.1 (last version for .NET framework 3.5)
13 | * Issue #48 Added `AnonymousFieldHarvester` for convenience
14 | * Issue #49 bugfix parsing brackets
15 | * Issue #51 bugfix thread problem
16 | * Performance improvements between 5%-40%
17 |
18 | ## v3.0
19 | * Issue #38 fixed root name
20 | * Issue #40 fixed output format for xml and json
21 | * Issue #46, changed namespace from "Stateprinter" to "Stateprinting" in order for the project to be usable from VB#
22 | * 10% speed up
23 | * Changed the namespace from "StatePrinter" to "StatePrinting" in order to be CLS compliant (cross language support)
24 |
25 |
26 |
27 | ## v2.2.xxx-pr
28 |
29 | * #43 Bugfix `\` in expected data failed on unit test rewrite due to lack of escaping.
30 |
31 |
32 | ## v2.2.281-pr
33 | * Caching run-time generated getter methods.
34 |
35 | ## v2.2.274-pr
36 |
37 | Added
38 | * Improved performance by 10% for curly and json outputformatters
39 | * #28 - General **50%-70% times speed up** of execution speed due to run-time code generation of reflection
40 | * #31 - `RollingGuidValueConverter` - Unit testing data containing Guid's just became much easier
41 | * Bugfixed the Json and Xml outputformatters when outputting dictionary/enumerables as the root element.
42 |
43 |
44 | ## v2.1.220
45 |
46 | Added
47 |
48 | * Functionality for controlling automatic test rewrite using an environment variable.
49 | * Functionality for including or excluding fields and properties based on one or more type descriptions. See `IncludeByType()` and `ExcludeByType()`.
50 | * Added `AreAlike()`, replacing `IsSame()` (which is deprecated). Similar story for `PrintAreAlike` replacing `PrintIsSame()`.
51 | * Made error message tell about `AreAlike()` when two strings are alike but not equals, when using `AreEquals()`.
52 | * Prepared for future expansion of functionality, by placing unit testing configuration in a sub-configuration class.
53 | * Obsoleted a lot of methods, describing the alternative API introduced in v2.1
54 |
55 |
56 | Fixed
57 |
58 | * [#22 Make error message configurable upon assertion failure](https://github.com/kbilsted/StatePrinter/issues/22)
59 |
60 |
61 |
62 |
63 | ## v2.0.169
64 |
65 | Added
66 |
67 | * Added automatic unit test rewriting
68 | * Added configuration of how line-endings are generated during state printing. This is to mitigate problems due to different operating systems uses different line-endings.
69 | * Added assertion helper methods `Stateprinter.Assert.AreEqual`, `Stateprinter.Assert.IsSame`, `Stateprinter.Assert.PrintIsSame` and `Stateprinter.Assert.That`. Improves the unit test experience by printing a suggested expected string as C# code.
70 | * Added a `AllFieldsAndPropertiesHarvester` which is able to harvest properties and fields.
71 | * `StringConverter` is now configurable with respect to quote character.
72 | * BREAKING CHANGE: Projective harvester is now using the `AllFieldsAndPropertiesHarvester` rather instead of the `FieldHarvester`. This means both fields and properties are now harvested.
73 |
74 |
75 | ## v1.0.6
76 |
77 | Added
78 |
79 | * Executing stylecop on the build server.
80 | * Made the `Configuration` class API a bit more fluent
81 | * BUGFIX: Harvesting of types were cached across `Stateprinter` instances, which no longer makes sense since harvesting is configurable from instance to instance.
82 | * BUGFIX: Changed how `ToString()` methods are harvested. Thanks to "Sjdirect".
83 |
84 |
85 | ## v1.0.5
86 |
87 | Added
88 |
89 | * Support for using the native `ToString()` implementation on types through a field harvester
90 | * Added a Projective field harvester to easily reduce the harvesting of selective fields on types in a type-safe manner. See the section on unit testing in the readme.md
91 | * Added the type `Stateprinter` and obsoleted the `StatePrinter` type
92 |
93 |
94 | ## v1.0.4
95 |
96 |
97 | Added
98 |
99 | * CLS compliance
100 | * 20% Performance boost
101 |
102 |
103 |
104 | Have fun!
105 |
106 | Kasper B. Graversen
107 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | All input welcome..
2 |
--------------------------------------------------------------------------------
/CreateNuget.cmd:
--------------------------------------------------------------------------------
1 | if not exist .\nuget_packages mkdir nuget_packages
2 | if not exist .\distro mkdir distro
3 | xcopy StatePrinter\*.cs distro\src\ /Y /Q /E
4 | xcopy StatePrinter\bin\Debug\*.dll distro\lib\net472\ /Q
5 | xcopy StatePrinter.nuspec distro\ /Q
6 | cd distro
7 |
8 | ..\.nuget\nuget.exe pack StatePrinter.nuspec -symbols -Prop Platform=AnyCPU
9 |
10 | xcopy *.nupkg ..\nuget_packages\ /Y /Q
11 |
12 | pause
13 | cd ..
14 | rmdir distro /s /q
15 |
--------------------------------------------------------------------------------
/DeployToNuget.cmd:
--------------------------------------------------------------------------------
1 | REM ON FIRST RUN, RUN THIS (change the key to whatever is found on your profile on www.nuget.org ->
2 | REM .nuget\NuGet.exe setapikey e39ea-get-the-full-key-on-nuget.org
3 |
4 | call CreateNuget.cmd
5 | .nuget\NuGet.exe push nuget_packages\StatePrinter.4.*.*.symbols.nupkg
6 | .nuget\NuGet.exe push nuget_packages\StatePrinter.4.*.*.nupkg
7 |
8 | cd nuget_packages
9 | del /q *
10 | cd ..
11 | rmdir nuget_packages
12 |
13 | pause
--------------------------------------------------------------------------------
/HowToRelease.txt:
--------------------------------------------------------------------------------
1 | Steps for creating a new release
2 | --------------------------------
3 |
4 | * Edit Stateprinter/Properties/Assemblyinfo.cs
5 | * Edit appveyor.yml
6 | * Update CHANGELOG.md
7 | * Update StatePrinter.nuspec
8 | * Run DeployToNuget.cmd
9 | * Add release on github.com
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #  StatePrinter
2 |
3 | [](https://ci.appveyor.com/project/kbilsted/stateprinter/branch/master)
4 | [](http://nuget.org/packages/stateprinter)
5 | [](http://nuget.org/packages/stateprinter)
6 | [](http://nuget.org/packages/stateprinter)
7 | [](https://coveralls.io/r/kbilsted/StatePrinter?branch=master)
8 | [](http://www.apache.org/licenses/LICENSE-2.0)
9 | []()
10 | []()
11 |
12 | [](https://gitter.im/kbilsted/StatePrinter?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
13 |
14 |
15 | **THIS PROJECT IS BEING SUPERSEDED BY *ReassureTest*! GO TO https://github.com/kbilsted/ReassureTest.Net**
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | # What is Stateprinter
24 | A simple framework for **automating** aspects of implementing `ToString()`-methods, unit testing, and debugging. Speed is achieved through **run-time code generation** and caching.
25 |
26 | Why you should take StatePrinter for a spin
27 |
28 | * *No more manual `ToString()`* - it is much easier to write robust and self-sufficient `ToString()` methods. Seamless integration into a code-base with manually implemented ToString-methods.
29 | * *No more manual writing/updating Asserts* - both for new tests and when the code changes, all tests can automatically be corrected.
30 | * *No more screens full of asserts*. Especially testing against object-graphs is a bliss.
31 | * Very configurable and extensible.
32 | * It is part of the back-end engine of other projects
33 | * The very nice [ApprovalTests framework](http://approvaltests.sourceforge.net/).
34 |
35 |
36 |
37 | ## How do I get started
38 |
39 | The documentation is split into
40 |
41 | * [Automating ToString methods](https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingToStrings.md)
42 |
43 | and
44 |
45 | * [Automating unit testing](https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingUnitTesting.md)
46 | * [The problems with traditional unit testing (that stateprinter solves)](https://github.com/kbilsted/StatePrinter/blob/master/doc/TheProblemsWithTraditionalUnitTesting.md)
47 |
48 | and
49 |
50 | * [Configuration and exension](https://github.com/kbilsted/StatePrinter/blob/master/doc/HowToConfigure.md)
51 |
52 |
53 | ## Where can I get it?
54 | Install Stateprinter from the package manager console:
55 |
56 | ```
57 | PM> Install-Package StatePrinter
58 | ```
59 |
60 | And for pre-release versions
61 |
62 | ```
63 | PM> Install-Package StatePrinter -Pre
64 | ```
65 |
66 |
67 | ## How can I get help?
68 | For quick questions, [Stack Overflow](http://stackoverflow.com/questions/tagged/stateprinter?sort=newest) is your best bet. For harder questions, bugs, issues or feature requests, [create a GitHub Issue (and let's chat)](https://github.com/kbilsted/StatePrinter/issues/new).
69 |
70 |
71 |
72 | ## How can I help out
73 | Everyone is encouraged to help improve this project. Here are a few ways you can help:
74 | * Blog about your experinces with the tool. We highly need publicity. I'll gladly link from here to your blog.
75 | * [Report bugs](https://github.com/kbilsted/StatePrinter/issues/new)
76 | * [Fix issues](https://github.com/kbilsted/StatePrinter/issues/) and submit pull requests
77 | * Write, clarify, or fix [the documentation](doc/)
78 | * [Suggest](https://github.com/kbilsted/StatePrinter/issues/new) or add new features
79 |
80 |
81 | *StatePrinter has been awarded a ReSharper group license, to share among all active contributers*.
82 |
83 |
84 |
85 | ## Versioning
86 | Stateprinter is maintained under the Semantic Versioning guidelines as much as possible. Releases will be numbered with the following format:
87 |
88 | `..`
89 |
90 | and constructed with the following guidelines:
91 |
92 | * Breaking backward compatibility bumps the major
93 | * New additions without breaking backward compatibility bumps the minor
94 | * Bug fixes and misc changes increase the build number
95 |
96 | For more information on SemVer, please visit http://semver.org/.
97 |
98 |
99 |
100 | ## History
101 | Version History: http://github.com/kbilsted/StatePrinter/blob/master/CHANGELOG.md
102 |
103 | This file describes the latest pushed changes. For documentation of earlier releases see:
104 | [1.0.6](https://github.com/kbilsted/StatePrinter/blob/1.0.6/README.md), [1.0.5](https://github.com/kbilsted/StatePrinter/blob/1.0.5/README.md), [1.0.4](https://github.com/kbilsted/StatePrinter/blob/1.0.4/README.md)
105 |
106 | Upgrading from v1.xx to v2.0.x should be a matter of configuring the `Configuration.LegacyBehaviour`
107 |
108 | Upgrading from v2.0 to v2.1 simply follow the documentation in the obsolete attributes.
109 |
110 |
111 |
112 |
113 | ## Requirements
114 | Requires .NET 3.5 or newer.
115 |
116 |
117 |
118 |
119 | ## License
120 | Stateprinter is under the Apache License 2.0, meaning that you can freely use this in other open source or commercial products. If you use it for commercial products please have the courtesy to leave me an email with a 'thank you'.
121 |
122 |
123 |
124 |
125 | **THIS PROJECT IS BEING SUPERSEDED BY *ReassureTest*! GO TO https://github.com/kbilsted/ReassureTest.Net**
126 |
127 |
128 |
129 |
130 | Have fun!
131 |
132 | Kasper B. Graversen
133 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/Configurations/ConfigurationTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 | using NUnit.Framework;
23 | using NUnit.Framework.Internal;
24 | using StatePrinting.Configurations;
25 | using StatePrinting.FieldHarvesters;
26 | using StatePrinting.TestAssistance;
27 | using StatePrinting.ValueConverters;
28 |
29 | namespace StatePrinting.Tests.Configurations
30 | {
31 | [TestFixture]
32 | class ConfigurationTest
33 | {
34 | [Test]
35 | public void TryFind()
36 | {
37 | var config = new Configuration();
38 | config.Add(new StandardTypesConverter(null));
39 |
40 | IValueConverter h;
41 | Assert.IsTrue(config.TryGetValueConverter(typeof(decimal), out h));
42 | Assert.IsTrue(h is StandardTypesConverter);
43 | }
44 |
45 | [Test]
46 | public void SettingNullValues()
47 | {
48 | var sut = new Configuration();
49 | Assert.Throws(() => sut.SetCulture(null));
50 | Assert.Throws(() => sut.SetIndentIncrement(null));
51 | Assert.Throws(() => sut.SetNewlineDefinition(null));
52 | Assert.Throws(() => sut.SetOutputFormatter(null));
53 |
54 | Assert.Throws(() => sut.Add((IFieldHarvester)null));
55 | Assert.Throws(() => sut.Add((IValueConverter)null));
56 |
57 | Assert.Throws(() => sut.AddHandler(null, t => new List()));
58 | Assert.Throws(() => sut.AddHandler(t => true, null));
59 | Assert.Throws(() => sut.AddHandler(null, null));
60 |
61 | Assert.Throws(() => sut.Test.SetAreEqualsMethod((TestFrameworkAreEqualsMethod) null));
62 | Assert.Throws(() => sut.Test.SetAutomaticTestRewrite(null));
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/ExamplesForDocumentation/ExampleEndlessAsserts.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using NUnit.Framework;
22 |
23 | namespace StatePrinting.Tests.ExamplesForDocumentation
24 | {
25 | [TestFixture]
26 | class ExampleListAndArrays
27 | {
28 |
29 | [Test]
30 | public void EndlessAssertsAlternative()
31 | {
32 | var allocation = new AllocationData
33 | {
34 | Premium = 22,
35 | FixedCosts = 23,
36 | PremiumCosts = 140,
37 | Tax = 110
38 | };
39 |
40 | var sut = new Allocator();
41 | var allocateData = sut.CreateAllocation(allocation);
42 |
43 | var printer = TestHelper.CreateTestPrinter();
44 | var expected = @"new AllocationDataResult()
45 | {
46 | Premium = 22
47 | OriginalDueDate = 01-01-2010 00:00:00
48 | Costs = new CostData()
49 | {
50 | MonthlyBillingFixedInternalCost = 38
51 | BillingInternalCost = 55
52 | MonthlyBillingFixedRunningRemuneration = 63
53 | MonthlyBillingFixedEstablishment = 53
54 | MonthlyBillingRegistration = 2
55 | }
56 | PremiumInternalCost = 1
57 | PremiumRemuneration = 2
58 | PremiumRegistration = 332
59 | PremiumEstablishment = 14
60 | PremiumInternalCostBeforeDiscount = 57
61 | PremiumInternalCostAfterDiscount = 37
62 | Tax = 110
63 | }";
64 | printer.Assert.PrintAreAlike(expected, allocateData);
65 | }
66 |
67 | }
68 |
69 | class Allocator
70 | {
71 | public AllocationDataResult CreateAllocation(AllocationData allocation)
72 | {
73 | var allocateData = new AllocationDataResult();
74 | allocateData.Premium = allocation.Premium;
75 |
76 | allocateData.OriginalDueDate = new DateTime(2010, 1, 1);
77 |
78 | allocateData.Costs = new CostData();
79 | allocateData.Costs.MonthlyBillingFixedInternalCost = 38;
80 | allocateData.Costs.BillingInternalCost = 55;
81 | allocateData.Costs.MonthlyBillingFixedRunningRemuneration = 63;
82 | allocateData.Costs.MonthlyBillingFixedEstablishment = 53;
83 | allocateData.Costs.MonthlyBillingRegistration = 2;
84 |
85 | allocateData.PremiumInternalCost = 1;
86 | allocateData.PremiumRemuneration = 2;
87 | allocateData.PremiumRegistration = 332;
88 | allocateData.PremiumEstablishment = 14;
89 |
90 | allocateData.PremiumInternalCostBeforeDiscount = 57;
91 | allocateData.PremiumInternalCostAfterDiscount = 37;
92 |
93 | allocateData.Tax = allocation.Tax;
94 | return allocateData;
95 | }
96 | }
97 |
98 | class AllocationDataResult
99 | {
100 | public int Premium { get; set; }
101 | public DateTime OriginalDueDate { get; set; }
102 | public CostData Costs { get; set; }
103 | public int PremiumInternalCost { get; set; }
104 | public int PremiumRemuneration { get; set; }
105 | public int PremiumRegistration { get; set; }
106 | public int PremiumEstablishment { get; set; }
107 | public int PremiumInternalCostBeforeDiscount { get; set; }
108 | public int PremiumInternalCostAfterDiscount { get; set; }
109 | public int Tax { get; set; }
110 | }
111 |
112 | class CostData
113 | {
114 | public int MonthlyBillingFixedInternalCost { get; set; }
115 | public int BillingInternalCost { get; set; }
116 | public int MonthlyBillingFixedRunningRemuneration { get; set; }
117 | public int MonthlyBillingFixedEstablishment { get; set; }
118 | public int MonthlyBillingRegistration { get; set; }
119 | }
120 |
121 | class AllocationData
122 | {
123 | public int Premium { get; set; }
124 | public int FixedCosts { get; set; }
125 | public int PremiumCosts { get; set; }
126 | public int Tax { get; set; }
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/ExamplesForDocumentation/ExampleListAndArrays.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 |
22 | namespace StatePrinting.Tests.ExamplesForDocumentation
23 | {
24 | [TestFixture]
25 | class ExampleEndlessAsserts
26 | {
27 |
28 | [Test]
29 | public void ExampleListAndArraysAlternative()
30 | {
31 | object products = 1;
32 | object vendors = 2;
33 | object year = 2222;
34 | int added1 = 0;
35 | int added2 = 0;
36 | int added3 = 0;
37 | var vendorManager = new TaxvendorManager(products, vendors, year);
38 | vendorManager.AddVendor(JobType.JobType1, added1);
39 | vendorManager.AddVendor(JobType.JobType2, added2);
40 | vendorManager.AddVendor(JobType.JobType3, added3);
41 |
42 | var expected = @"new VendorAllocation[]()
43 | {
44 | [0] = new VendorAllocation()
45 | {
46 | Allocation = 100
47 | Price = 20
48 | Share = 20
49 | }
50 | [1] = new VendorAllocation()
51 | {
52 | Allocation = 120
53 | Price = 550
54 | Share = 30
55 | }
56 | [2] = new VendorAllocation()
57 | {
58 | Allocation = 880
59 | Price = 11
60 | Share = 50
61 | }
62 | }";
63 |
64 | TestHelper.Assert().PrintAreAlike(expected, vendorManager.VendorJobSplit);
65 | }
66 | }
67 |
68 |
69 | enum JobType
70 | {
71 | JobType3,
72 | JobType2,
73 | JobType1
74 | }
75 |
76 | class TaxvendorManager
77 | {
78 | public TaxvendorManager(object products, object vendors, object year)
79 | {
80 | VendorJobSplit = new VendorAllocation[]
81 | {
82 | new VendorAllocation(){Allocation = 100, Price = 20, Share = 20f},
83 | new VendorAllocation(){Allocation = 120, Price = 550, Share = 30f},
84 | new VendorAllocation(){Allocation = 880, Price = 11, Share = 50f},
85 | };
86 | }
87 |
88 | public VendorAllocation[] VendorJobSplit { get; set; }
89 |
90 | public void AddVendor(object jobType3, object added3)
91 | {
92 |
93 | }
94 | }
95 |
96 | class VendorAllocation
97 | {
98 | public int Allocation, Price;
99 | public float Share;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/ExamplesForDocumentation/ExampleOnSimpleAsserts.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Globalization;
21 | using NUnit.Framework;
22 | using StatePrinting.TestAssistance;
23 | using StatePrinting.ValueConverters;
24 |
25 | namespace StatePrinting.Tests.ExamplesForDocumentation
26 | {
27 | [TestFixture]
28 | class ExampleOnSimpleAsserts
29 | {
30 | #region hidden
31 | object a = 1, b = 2, c = 3, d = 4;
32 | #endregion
33 |
34 | public Stateprinter CreatePrinter()
35 | {
36 | var printer = new Stateprinter();
37 | printer.Configuration
38 | .SetCulture(CultureInfo.CreateSpecificCulture("en-US"))
39 | .Test.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
40 | .Test.SetAutomaticTestRewrite(filename => new EnvironmentReader().UseTestAutoRewrite())
41 | .Test.SetAutomaticTestRewrite(filename => true)
42 | .Add(new StringConverter(""));
43 |
44 | return printer;
45 | }
46 |
47 | [Test]
48 | public void TestName()
49 | {
50 | var printer = CreatePrinter();
51 | var sut = new AmountSplitter();
52 | var actual = sut.Split(100, 3);
53 |
54 | printer.Assert.AreEqual("3", printer.PrintObject(actual.Length));
55 | printer.Assert.AreEqual("33.333333333333333333333333333", printer.PrintObject(actual[0]));
56 | printer.Assert.AreEqual("33.333333333333333333333333333", printer.PrintObject(actual[1]));
57 | printer.Assert.AreEqual("33.333333333333333333333333333", printer.PrintObject(actual[2]));
58 | }
59 |
60 | [Test]
61 | public void TestImprovedSyntax()
62 | {
63 | var assert = CreatePrinter().Assert;
64 | var sut = new AmountSplitter();
65 | var actual = sut.Split(100, 3);
66 |
67 | assert.PrintEquals("3", actual.Length);
68 | assert.PrintEquals("33.333333333333333333333333333", actual[0]);
69 | assert.PrintEquals("33.333333333333333333333333333", actual[1]);
70 | assert.PrintEquals("33.333333333333333333333333333", actual[2]);
71 | }
72 |
73 |
74 |
75 | [Test]
76 | public void TestProcessOrder()
77 | {
78 | var printer = CreatePrinter();
79 |
80 | var sut = new OrderProcessor(a, b);
81 | var actual = sut.Process(c, d);
82 |
83 | printer.Assert.AreEqual("1", printer.PrintObject(actual.OrderNumber));
84 | printer.Assert.AreEqual("X-mas present", printer.PrintObject(actual.OrderDescription));
85 | printer.Assert.AreEqual("43", printer.PrintObject(actual.Total));
86 | }
87 |
88 | [Test]
89 | public void TestProcessOrderImproved()
90 | {
91 | var assert = CreatePrinter().Assert;
92 |
93 | var sut = new OrderProcessor(a, b);
94 | var actual = sut.Process(c, d);
95 |
96 | assert.PrintEquals("1", actual.OrderNumber);
97 | assert.PrintEquals("X-mas present", actual.OrderDescription);
98 | assert.PrintEquals("43", actual.Total);
99 | }
100 |
101 |
102 | class AmountSplitter
103 | {
104 | public decimal[] Split(decimal amount, int parts)
105 | {
106 | var moneyBags = new decimal[parts];
107 | for (int i = 0; i < parts; i++)
108 | moneyBags[i] = amount / parts;
109 | return moneyBags;
110 | }
111 | }
112 |
113 | class OrderProcessor
114 | {
115 | public OrderProcessor(object a, object b)
116 | {
117 | }
118 |
119 | public Order Process(object c, object d)
120 | {
121 | return new Order(1, "X-mas present");
122 | }
123 | }
124 | }
125 |
126 | class Order
127 | {
128 | public int OrderNumber;
129 | public string OrderDescription;
130 | public decimal Total = 43;
131 |
132 | public Order(int orderNumber, string orderDescription)
133 | {
134 | this.OrderNumber = orderNumber;
135 | this.OrderDescription = orderDescription;
136 | }
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/FieldHarvesters/AllHarvesterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Linq;
21 | using NUnit.Framework;
22 | using StatePrinting.FieldHarvesters;
23 | using StatePrinting.Tests.IntegrationTests;
24 |
25 | namespace StatePrinting.Tests.FieldHarvesters
26 | {
27 | [TestFixture]
28 | class AllHarvesterTest
29 | {
30 | HarvestHelper helper = new HarvestHelper();
31 |
32 | [Test]
33 | public void AllFieldsHarvestTest()
34 | {
35 | var harvester = new AllFieldsHarvester();
36 |
37 | var fields = harvester.GetFields(typeof(Car)).Select(x => x.SanitizedName).ToArray();
38 | CollectionAssert.AreEquivalent(new[] { "StereoAmplifiers", "steeringWheel", "Brand" }, fields);
39 |
40 | fields = harvester.GetFields(typeof(SteeringWheel)).Select(x => x.SanitizedName).ToArray();
41 | CollectionAssert.AreEquivalent(new[] { "Size", "Grip", "Weight" }, fields);
42 | }
43 |
44 | [Test]
45 | public void PublicFieldsHarvesterTest()
46 | {
47 | var harvester = new PublicFieldsHarvester();
48 |
49 | var fields = harvester.GetFields(typeof(Car)).Select(x => x.SanitizedName).ToArray();
50 | CollectionAssert.AreEquivalent(new[] { "Brand" }, fields);
51 |
52 | fields = harvester.GetFields(typeof(SteeringWheel)).Select(x => x.SanitizedName).ToArray();
53 | CollectionAssert.AreEquivalent(new string[0] { }, fields);
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/FieldHarvesters/AnonymousHarvesterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 | using NUnit.Framework;
23 | using StatePrinting.Configurations;
24 | using StatePrinting.FieldHarvesters;
25 |
26 | namespace StatePrinting.Tests.FieldHarvesters
27 | {
28 | ///
29 | /// Shows how the can be used
30 | ///
31 | [TestFixture]
32 | class AnonymousHarvesterTest
33 | {
34 | class A
35 | {
36 | public string Name;
37 | }
38 |
39 | class B
40 | {
41 | public int Age;
42 | }
43 |
44 | [Test]
45 | public void SpecializedClassHandlerHandledClass()
46 | {
47 | Configuration cfg = ConfigurationHelper.GetStandardConfiguration(" ");
48 | AddAnonymousHandler(cfg);
49 | var sut = new Stateprinter(cfg);
50 |
51 | var expected = @"new B()
52 | {
53 | Age = ""Its age is 1""
54 | }";
55 |
56 | var actual = sut.PrintObject(new B { Age = 1 });
57 | Assert.AreEqual(expected, actual);
58 | }
59 |
60 | [Test]
61 | public void SpecializedClassHandlerNotHandledClass()
62 | {
63 | var cfg = new Configuration(" ");
64 | AddAnonymousHandler(cfg);
65 |
66 | var sut = new Stateprinter(cfg);
67 |
68 | Assert.Throws(() => sut.PrintObject(new A { Name = "MyName" }), "");
69 | }
70 |
71 | private void AddAnonymousHandler(Configuration cfg)
72 | {
73 | cfg.AddHandler(
74 | t => t == typeof(B),
75 | t => new List { new SanitizedFieldInfo(null, "Age", o => "Its age is " + ((B)o).Age) });
76 | }
77 | }
78 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/FieldHarvesters/ProjectionHarvesterByTypeTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 | using StatePrinting.TestAssistance;
22 |
23 | namespace StatePrinting.Tests.FieldHarvesters
24 | {
25 | [TestFixture]
26 | public class TypeFilteringTests
27 | {
28 | interface IA
29 | {
30 | int A { get; set; }
31 | }
32 |
33 | interface IB
34 | {
35 | int B { get; set; }
36 | }
37 |
38 | interface IC
39 | {
40 | int C { get; set; }
41 | }
42 |
43 | interface ID
44 | {
45 | int D { get; set; }
46 | }
47 |
48 | class AtoD : IA, IB, IC, ID
49 | {
50 | public int A { get; set; }
51 | public int B { get; set; }
52 | public int C { get; set; }
53 | public int D { get; set; }
54 |
55 | public AtoD()
56 | {
57 | A = 1;
58 | B = 2;
59 | C = 3;
60 | D = 4;
61 | }
62 | }
63 |
64 | [Test]
65 | public void TestIncludeByType()
66 | {
67 | var sut = new AtoD();
68 | Asserter assert;
69 |
70 | assert = TestHelper.CreateShortAsserter();
71 | assert.PrintEquals("new AtoD() { A = 1 B = 2 C = 3 D = 4 }", sut);
72 |
73 | assert = TestHelper.CreateShortAsserter();
74 | assert.Project.IncludeByType();
75 | assert.PrintEquals("new AtoD() { A = 1 }", sut);
76 |
77 | assert = TestHelper.CreateShortAsserter();
78 | assert.Project.IncludeByType();
79 | assert.PrintEquals("new AtoD() { A = 1 B = 2 }", sut);
80 |
81 | assert = TestHelper.CreateShortAsserter();
82 | assert.Project.IncludeByType();
83 | assert.PrintEquals("new AtoD() { A = 1 B = 2 C = 3 }", sut);
84 |
85 | assert = TestHelper.CreateShortAsserter();
86 | assert.Project.IncludeByType();
87 | assert.PrintEquals("new AtoD() { A = 1 B = 2 C = 3 D = 4 }", sut);
88 | }
89 |
90 | [Test]
91 | public void TestExcludeByType()
92 | {
93 | var sut = new AtoD();
94 | Asserter assert;
95 |
96 | assert = TestHelper.CreateShortAsserter();
97 | assert.PrintEquals("new AtoD() { A = 1 B = 2 C = 3 D = 4 }", sut);
98 |
99 | assert = TestHelper.CreateShortAsserter();
100 | assert.Project.ExcludeByType();
101 | assert.PrintEquals("new AtoD() { B = 2 C = 3 D = 4 }", sut);
102 |
103 | assert = TestHelper.CreateShortAsserter();
104 | assert.Project.ExcludeByType();
105 | assert.PrintEquals("new AtoD() { C = 3 D = 4 }", sut);
106 |
107 | assert = TestHelper.CreateShortAsserter();
108 | assert.Project.ExcludeByType();
109 | assert.PrintEquals("new AtoD() { D = 4 }", sut);
110 |
111 | assert = TestHelper.CreateShortAsserter();
112 | assert.Project.ExcludeByType();
113 | assert.PrintEquals("new AtoD() { }", sut);
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/FieldHarvesters/ToStringAwareHarvesterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 | using StatePrinting.Configurations;
22 | using StatePrinting.FieldHarvesters;
23 |
24 | namespace StatePrinting.Tests.FieldHarvesters
25 | {
26 | ///
27 | /// Shows how the can be used
28 | ///
29 | [TestFixture]
30 | class ToStringAwareHarvesterTest
31 | {
32 | class A
33 | {
34 | public int X;
35 |
36 | int somePrivateVariable;
37 | //should not be printed since the PublicFieldsHarvester should be used if there is no explicit ToString()
38 |
39 | public B b = new B() { Age = 2 };
40 | public void Dummy()
41 | {
42 | somePrivateVariable++;
43 | }
44 | }
45 |
46 | class B
47 | {
48 | public int Age;
49 |
50 | public override string ToString()
51 | {
52 | return "My age is " + Age;
53 | }
54 | }
55 |
56 | class C : B
57 | {
58 | public C()
59 | {
60 | Age = 42;
61 | }
62 | }
63 |
64 | [Test]
65 | public void Userstory_PrintUseToString_WhenDirectlyAvailable()
66 | {
67 | var sut = CreatePrinter();
68 | var expected = @"new B()
69 | {
70 | ToString() = ""My age is 1""
71 | }";
72 | var actual = sut.PrintObject(new B { Age = 1 });
73 | Assert.AreEqual(expected, actual);
74 | }
75 |
76 | [Test]
77 | public void Userstory_PrintUseToString_WhenAvailable()
78 | {
79 | var sut = CreatePrinter();
80 | var expected = @"new A()
81 | {
82 | X = 1
83 | b = new B()
84 | {
85 | ToString() = ""My age is 2""
86 | }
87 | }";
88 | var actual = sut.PrintObject(new A { X = 1 });
89 | Assert.AreEqual(expected, actual);
90 | }
91 |
92 |
93 |
94 | [Test]
95 | public void Userstory_PrintDontUseToString_WhenInherited()
96 | {
97 | var sut = CreatePrinter();
98 | var expected = @"new A()
99 | {
100 | X = 1
101 | b = new C()
102 | {
103 | Age = 42
104 | }
105 | }";
106 | var actual = sut.PrintObject(new A { X = 1, b = new C() });
107 | Assert.AreEqual(expected, actual);
108 | }
109 |
110 | Stateprinter CreatePrinter()
111 | {
112 | Configuration cfg = ConfigurationHelper.GetStandardConfiguration(" ");
113 | cfg.Add(new PublicFieldsHarvester());
114 | cfg.Add(new ToStringAwareHarvester());
115 |
116 | var sut = new Stateprinter(cfg);
117 | return sut;
118 | }
119 | }
120 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/CallStackReflectorTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using NUnit.Framework;
22 | using StatePrinting.TestAssistance;
23 |
24 | namespace StatePrinting.Tests.IntegrationTests
25 | {
26 | [TestFixture]
27 | class CallStackReflectorTest
28 | {
29 | [Test]
30 | public void TryGetInfo_inside_test_method()
31 | {
32 | var res = new CallStackReflector().TryGetLocation();
33 |
34 | Assert.IsTrue(res.Filepath.EndsWith("ReflectorTest.cs"));
35 | Assert.AreEqual(32, res.LineNumber);
36 | }
37 |
38 | [Test]
39 | public void TryGetInfo_inside_lambda_expected_outside_lambda()
40 | {
41 | UnitTestLocationInfo res = null;
42 |
43 | Action x = () => res = new CallStackReflector().TryGetLocation();
44 | x();
45 |
46 | Assert.IsTrue(res.Filepath.EndsWith("ReflectorTest.cs"));
47 | Assert.AreEqual(43, res.LineNumber);
48 | }
49 |
50 | [Test]
51 | public void TryGetInfo_inside_AssertThrowsLambda_expected_outside_lambda()
52 | {
53 | UnitTestLocationInfo res = null;
54 |
55 | Assert.DoesNotThrow(() => res = new CallStackReflector().TryGetLocation());
56 |
57 | Assert.IsTrue(res.Filepath.EndsWith("ReflectorTest.cs"));
58 | Assert.AreEqual(55, res.LineNumber);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/CultureTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Globalization;
22 | using NUnit.Framework;
23 |
24 | namespace StatePrinting.Tests.IntegrationTests
25 | {
26 | [TestFixture]
27 | class CultureTests
28 | {
29 | const decimal DecimalNumber = 12345.343M;
30 | readonly DateTime dateTime = new DateTime(2010, 2, 28, 22, 10, 59);
31 |
32 | [Test]
33 | public void CultureDependentPrinting_us()
34 | {
35 | var usPrinter = new Stateprinter();
36 | usPrinter.Configuration.Culture = new CultureInfo("en-US");
37 |
38 | Assert.AreEqual("12345.343", usPrinter.PrintObject(DecimalNumber));
39 | Assert.AreEqual("12345.34", usPrinter.PrintObject((float)DecimalNumber));
40 | Assert.AreEqual("2/28/2010 10:10:59 PM", usPrinter.PrintObject(dateTime));
41 | }
42 |
43 | [Test]
44 | public void CultureDependentPrinting_dk()
45 | {
46 | var dkPrinter = new Stateprinter();
47 | dkPrinter.Configuration.Culture = new CultureInfo("da-DK");
48 |
49 | Assert.AreEqual("12345,343", dkPrinter.PrintObject(DecimalNumber));
50 | Assert.AreEqual("12345,34", dkPrinter.PrintObject((float)DecimalNumber));
51 | Assert.AreEqual("28-02-2010 22:10:59", dkPrinter.PrintObject(dateTime));
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/InheritanceTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 |
22 | namespace StatePrinting.Tests.IntegrationTests
23 | {
24 | [TestFixture]
25 | class InheritanceTest
26 | {
27 | [Test]
28 | public void HarvestSameFieldsInTypAndSubtype()
29 | {
30 | B b = new B();
31 | ((A)b).SomeFieldOnlyInA = 1;
32 | ((A)b).SameFieldInAB = "A part";
33 | b.SomeFieldOnlyInB = 2;
34 | b.SameFieldInAB = "B part";
35 |
36 | var expected = @"new B()
37 | {
38 | SomeFieldOnlyInA = 1
39 | SameFieldInAB = ""A part""
40 | SomeFieldOnlyInB = 2
41 | SameFieldInAB = ""B part""
42 | }";
43 | TestHelper.Assert().PrintAreAlike(expected, (B) b);
44 | TestHelper.Assert().PrintAreAlike(expected, (A) b);
45 | }
46 |
47 |
48 | class A
49 | {
50 | public int SomeFieldOnlyInA;
51 | public string SameFieldInAB;
52 | }
53 |
54 |
55 | class B : A
56 | {
57 | public int SomeFieldOnlyInB;
58 | public new string SameFieldInAB;
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/PublicPropertiesTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Collections;
21 | using System.Collections.Generic;
22 | using NUnit.Framework;
23 | using StatePrinting.Configurations;
24 | using StatePrinting.FieldHarvesters;
25 |
26 | namespace StatePrinting.Tests.IntegrationTests
27 | {
28 | [TestFixture]
29 | class PublicPropertiesTest
30 | {
31 | class GetterOnly
32 | {
33 | public int i;
34 |
35 | int j = 22;
36 |
37 | public int Sum
38 | {
39 | get
40 | {
41 | return i + j;
42 | }
43 | }
44 |
45 | internal int Sum2
46 | {
47 | get
48 | {
49 | return i + j;
50 | }
51 | }
52 | }
53 |
54 | class SetterOnly
55 | {
56 | public int i, j;
57 |
58 | public int Sum
59 | {
60 | set
61 | {
62 | i = value;
63 | }
64 | }
65 | }
66 |
67 | class GetterSetter
68 | {
69 | public int I { get; set; }
70 | int J { get; set; }
71 | int K { get; set; }
72 | }
73 |
74 | class GetterSetterExplicitBackingField
75 | {
76 | int i, j;
77 |
78 | public int I
79 | {
80 | get
81 | {
82 | return i;
83 | }
84 | set
85 | {
86 | i = value;
87 | }
88 | }
89 |
90 | internal int J
91 | {
92 | get
93 | {
94 | return j;
95 | }
96 | set
97 | {
98 | j = value;
99 | }
100 | }
101 | }
102 |
103 | class IndexedProperty
104 | {
105 | public int i, j;
106 |
107 | public int this[int index]
108 | {
109 | get
110 | {
111 | if (index == 1) return i;
112 | if (index == 2) return j;
113 | return -1;
114 | }
115 | }
116 | }
117 |
118 | [Test]
119 | public void GetterOnly_IsIncluded()
120 | {
121 | var sut = new GetterOnly() { i = 1 };
122 | var printer = CreatePrinter();
123 | Assert.AreEqual(@"new GetterOnly()
124 | {
125 | Sum = 23
126 | i = 1
127 | }", printer.PrintObject(sut, ""));
128 | }
129 |
130 |
131 | [Test]
132 | public void SetterOnly_NotIncluded()
133 | {
134 | var sut = new SetterOnly() { i = 1, j = 2 };
135 | var printer = CreatePrinter();
136 | Assert.AreEqual(@"new SetterOnly()
137 | {
138 | i = 1
139 | j = 2
140 | }", printer.PrintObject(sut, ""));
141 | }
142 |
143 |
144 | [Test]
145 | public void GetterSetterOnly_IsIncluded()
146 | {
147 | var sut = new GetterSetter() { I = 1 };
148 | var printer = CreatePrinter();
149 |
150 | Assert.AreEqual(@"new GetterSetter()
151 | {
152 | I = 1
153 | }", printer.PrintObject(sut, ""));
154 | }
155 |
156 |
157 | ///
158 | /// unfortunately both are printed. A is needed in order to reduce the number of fields.
159 | ///
160 | /// We see this kind of implementation in
161 | ///
162 | /// and
163 | ///
164 | [Test]
165 | public void GetterSetter_WithExplicitBackingField_BothAreIncluded()
166 | {
167 | var sut = new GetterSetterExplicitBackingField() { I = 1, J = 2 };
168 | var printer = CreatePrinter();
169 |
170 | Assert.AreEqual(@"new GetterSetterExplicitBackingField()
171 | {
172 | I = 1
173 | }", printer.PrintObject(sut, ""));
174 | }
175 |
176 |
177 | [Test]
178 | public void GetterIndexedProperty_not_included()
179 | {
180 | var sut = new IndexedProperty() { i = 1, j = 2 };
181 | var printer = CreatePrinter();
182 |
183 | Assert.AreEqual(@"new IndexedProperty()
184 | {
185 | i = 1
186 | j = 2
187 | }", printer.PrintObject(sut, ""));
188 | }
189 |
190 |
191 | Stateprinter CreatePrinter()
192 | {
193 | return
194 | new Stateprinter(
195 | ConfigurationHelper.GetStandardConfiguration()
196 | .Add(new PublicFieldsAndPropertiesHarvester()));
197 | }
198 | }
199 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/ToStringMethodTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 |
22 | namespace StatePrinting.Tests.IntegrationTests
23 | {
24 | ///
25 | /// An example of using the state printer as a generic ToString() implementation.
26 | ///
27 | [TestFixture]
28 | class ToStringMethodTest
29 | {
30 | [Test]
31 | public void TestToStringMethod()
32 | {
33 | var a = new AClassWithToString();
34 | string expected =
35 | @"new AClassWithToString()
36 | {
37 | B = ""hello""
38 | C = new Int32[]()
39 | {
40 | [0] = 5
41 | [1] = 4
42 | [2] = 3
43 | [3] = 2
44 | [4] = 1
45 | }
46 | }";
47 | Assert.AreEqual(expected, a.ToString());
48 | }
49 | }
50 |
51 |
52 | class AClassWithToString
53 | {
54 | string B = "hello";
55 | int[] C = { 5, 4, 3, 2, 1 };
56 | static readonly Stateprinter printer = new Stateprinter();
57 |
58 | public override string ToString()
59 | {
60 | return printer.PrintObject(this);
61 | }
62 |
63 | public void Dummy()
64 | {
65 | B = B + " ";
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/IntegrationTests/TwoDimensionArrayTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 | using StatePrinting.OutputFormatters;
22 |
23 | namespace StatePrinting.Tests.IntegrationTests
24 | {
25 | [TestFixture]
26 | class TwoDimensionArrayTest
27 | {
28 | static readonly int[,] twoDimArray = { { 1, 2 }, { 3, 4 } };
29 |
30 | [TestFixture]
31 | class ArrayTestCurly
32 | {
33 | string expected = @"new Int32[,]()
34 | {
35 | [0] = 1
36 | [1] = 2
37 | [2] = 3
38 | [3] = 4
39 | }";
40 |
41 | [Test]
42 | public void TwoDimArray()
43 | {
44 | var printer = new Stateprinter();
45 | Assert.AreEqual(expected, printer.PrintObject(twoDimArray, ""));
46 | }
47 |
48 | [Test]
49 | public void TwoDimArray_LegacyApi()
50 | {
51 | var printer = new Stateprinter();
52 | printer.Configuration.LegacyBehaviour.TrimTrailingNewlines = false;
53 | Assert.AreEqual(expected + "\r\n", printer.PrintObject(twoDimArray, ""));
54 | }
55 | }
56 |
57 |
58 | [TestFixture]
59 | class ArrayTestJson
60 | {
61 |
62 | [Test]
63 | public void TwoDimArray()
64 | {
65 | var printer = TestHelper.CreateTestPrinter();
66 | printer.Configuration.SetOutputFormatter(new JsonStyle(printer.Configuration));
67 |
68 | var expected = @"[
69 | 1,
70 | 2,
71 | 3,
72 | 4
73 | ]";
74 | printer.Assert.PrintEquals(expected, twoDimArray);
75 | }
76 | }
77 |
78 | [TestFixture]
79 | class ArrayTestXml
80 | {
81 |
82 | [Test]
83 | public void TwoDimArray()
84 | {
85 | var printer = TestHelper.CreateTestPrinter();
86 | printer.Configuration.SetOutputFormatter(new XmlStyle(printer.Configuration));
87 |
88 | var expected = @"
89 | 1
90 | 2
91 | 3
92 | 4
93 | ";
94 | printer.Assert.PrintEquals(expected, twoDimArray);
95 | }
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/Introspection/ReferenceTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 | using StatePrinting.Introspection;
22 |
23 | namespace StatePrinting.Tests.Introspection
24 | {
25 | [TestFixture]
26 | class ReferenceTest
27 | {
28 | [Test]
29 | public void Equals()
30 | {
31 | var a = new Reference(1);
32 | var b = new Reference(1);
33 | Assert.AreEqual(a, a);
34 | Assert.AreEqual(a, b);
35 | Assert.IsTrue(a.Equals((object) b));
36 | }
37 |
38 | [Test]
39 | public void NotEquals()
40 | {
41 | var a = new Reference(1);
42 | var b = new Reference(2);
43 | Assert.AreNotEqual(a, b);
44 | Assert.AreNotEqual(a, null);
45 | }
46 |
47 | [Test]
48 | public void TestToString()
49 | {
50 | var a = new Reference(1);
51 | Assert.AreEqual("1", a.ToString());
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/Introspection/TokenTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Collections.Generic;
21 | using System.Linq;
22 | using NUnit.Framework;
23 | using StatePrinting.Introspection;
24 | using StatePrinting.OutputFormatters;
25 |
26 | namespace StatePrinting.Tests.Introspection
27 | {
28 | [TestFixture]
29 | class TokenTest
30 | {
31 | [Test]
32 | public void Equals()
33 | {
34 | var a = new Token(TokenType.StartScope);
35 | var b = new Token(TokenType.StartScope);
36 | Assert.AreEqual(a, a);
37 | Assert.AreEqual(a, b);
38 | Assert.IsFalse(a.Equals((Token)null));
39 | Assert.IsTrue(a.Equals(a));
40 | Assert.IsTrue(a.Equals((object)b));
41 | }
42 |
43 | [Test]
44 | public void NotEquals()
45 | {
46 | var a = new Token(TokenType.StartScope);
47 | var b = new Token(TokenType.EndScope);
48 | Assert.AreNotEqual(a, b);
49 | Assert.AreNotEqual(a, null);
50 |
51 | b = new Token(TokenType.StartScope, reference: new Reference(2));
52 | Assert.AreNotEqual(a, b);
53 | }
54 | }
55 |
56 | ///
57 | /// Outputformatter that can show what has been introspected. For unit testing only
58 | ///
59 | public class TokenOutputter : IOutputFormatter
60 | {
61 | public List IntrospectedTokens;
62 |
63 | public string Print(List tokens)
64 | {
65 | IntrospectedTokens = tokens.ToList();
66 | return "The result of this outputter is found in the field 'IntrospectedTokens'";
67 | }
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/Mocks/Mocks.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using StatePrinting.TestAssistance;
21 |
22 | namespace StatePrinting.Tests.Mocks
23 | {
24 | class AreEqualsMethodMock
25 | {
26 | public string Expected { get; private set; }
27 | public string Actual { get; private set; }
28 | public string Message { get; private set; }
29 |
30 | public void AreEqualsMock(string exp, string actual, string msg)
31 | {
32 | Expected = exp;
33 | Actual = actual;
34 | Message = msg;
35 | }
36 | }
37 |
38 | class FileRepositoryMock : FileRepository
39 | {
40 | byte[] whatToRead;
41 | public string WritePath { get; private set; }
42 | public byte[] WriteContent { get; private set; }
43 |
44 | public FileRepositoryMock(byte[] whatToRead)
45 | {
46 | this.whatToRead = whatToRead;
47 | }
48 |
49 | public override byte[] Read(string path)
50 | {
51 | return whatToRead;
52 | }
53 |
54 | public override void Write(string path, byte[] content)
55 | {
56 | WritePath = path;
57 | WriteContent = content;
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/OutputFormatters/RollingGuidValueConverterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using NUnit.Framework;
22 | using StatePrinting.ValueConverters;
23 |
24 | namespace StatePrinting.Tests.OutputFormatters
25 | {
26 | [TestFixture]
27 | class RollingGuidValueConverterTest
28 | {
29 | [Test]
30 | public void TestRolling()
31 | {
32 | var sut = new RollingGuidValueConverter();
33 | Assert.AreEqual("00000000-0000-0000-0000-000000000001", sut.Convert(Guid.NewGuid()));
34 | Assert.AreEqual("00000000-0000-0000-0000-000000000002", sut.Convert(Guid.NewGuid()));
35 | Assert.AreEqual("00000000-0000-0000-0000-000000000003", sut.Convert(Guid.NewGuid()));
36 | Assert.AreEqual("00000000-0000-0000-0000-000000000004", sut.Convert(Guid.NewGuid()));
37 | Assert.AreEqual("00000000-0000-0000-0000-000000000005", sut.Convert(Guid.NewGuid()));
38 | Assert.AreEqual("00000000-0000-0000-0000-000000000006", sut.Convert(Guid.NewGuid()));
39 | Assert.AreEqual("00000000-0000-0000-0000-000000000007", sut.Convert(Guid.NewGuid()));
40 | Assert.AreEqual("00000000-0000-0000-0000-000000000008", sut.Convert(Guid.NewGuid()));
41 | Assert.AreEqual("00000000-0000-0000-0000-000000000009", sut.Convert(Guid.NewGuid()));
42 | Assert.AreEqual("00000000-0000-0000-0000-000000000010", sut.Convert(Guid.NewGuid()));
43 | }
44 |
45 | [Test]
46 | public void TestReuseRolledValues()
47 | {
48 | var sut = new RollingGuidValueConverter();
49 | Guid g1 = Guid.NewGuid();
50 | Guid g2 = Guid.NewGuid();
51 | Assert.AreEqual("00000000-0000-0000-0000-000000000001", sut.Convert(g1));
52 | Assert.AreEqual("00000000-0000-0000-0000-000000000002", sut.Convert(g2));
53 | Assert.AreEqual("00000000-0000-0000-0000-000000000001", sut.Convert(g1));
54 | Assert.AreEqual("00000000-0000-0000-0000-000000000002", sut.Convert(g2));
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/OutputFormatters/StringBuilderTrimmerTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Text;
21 | using NUnit.Framework;
22 | using StatePrinting.OutputFormatters;
23 |
24 | namespace StatePrinting.Tests.OutputFormatters
25 | {
26 | [TestFixture]
27 | class StringBuilderTrimmerTest
28 | {
29 | [Test]
30 | public void TestTrimLast_Empty()
31 | {
32 | var sb = new StringBuilder("");
33 | Assert.AreEqual(0, new StringBuilderTrimmer(true).TrimLast(sb));
34 | }
35 |
36 | [Test]
37 | public void TestTrimLast_NothingToTrim()
38 | {
39 | var sb = new StringBuilder("abvc");
40 | Assert.AreEqual(0, new StringBuilderTrimmer(true).TrimLast(sb));
41 | }
42 |
43 | [Test]
44 | public void TestTrimLast_TrimSpaces()
45 | {
46 | var sb = new StringBuilder("abvc ");
47 | Assert.AreEqual(2, new StringBuilderTrimmer(true).TrimLast(sb));
48 | }
49 |
50 | [Test]
51 | public void TestTrimLast_TrimAllSpaces()
52 | {
53 | var sb = new StringBuilder(" ");
54 | Assert.AreEqual(3, new StringBuilderTrimmer(true).TrimLast(sb));
55 | Assert.AreEqual(3, new StringBuilderTrimmer(false).TrimLast(sb));
56 | }
57 | [Test]
58 | public void TestTrimLast_TrimAllNewlines()
59 | {
60 | var sb = new StringBuilder(" \r\n");
61 | Assert.AreEqual(2, new StringBuilderTrimmer(true).TrimLast(sb));
62 | Assert.AreEqual(0, new StringBuilderTrimmer(false).TrimLast(sb));
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/OutputFormatters/TokenFilterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Collections.Generic;
21 | using NUnit.Framework;
22 | using StatePrinting.Introspection;
23 | using StatePrinting.OutputFormatters;
24 |
25 | namespace StatePrinting.Tests.OutputFormatters
26 | {
27 | [TestFixture]
28 | class TokenFilterTest
29 | {
30 |
31 | [Test]
32 | public void GetHashcode()
33 | {
34 | var sut = new Token(TokenType.FieldnameWithTypeAndReference, null, null, null, null);
35 | Assert.AreEqual(1156279432, sut.GetHashCode());
36 | }
37 |
38 | [Test]
39 | public void Transform_noncycle()
40 | {
41 | var nonCycleTokens = new List()
42 | {
43 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldA"), "value1", new Reference(1), typeof(string) ),
44 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldB"), "value2", new Reference(2), typeof(string)),
45 | };
46 |
47 | var filter = new UnusedReferencesTokenFilter();
48 | var newlist = filter.FilterUnusedReferences(nonCycleTokens);
49 |
50 | // test
51 | var expected = new List()
52 | {
53 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldA"), "value1", null, typeof(string)),
54 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldB"), "value2", null, typeof(string)),
55 | };
56 |
57 | CollectionAssert.AreEqual(expected, newlist);
58 | }
59 |
60 | [Test]
61 | public void Transform_cycle()
62 | {
63 | var nonCycleTokens = new List()
64 | {
65 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldA"), "value1", new Reference(0), typeof(string)),
66 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldB"), "value2", new Reference(1), typeof(int)),
67 | Token.SeenBefore(new Field("FieldB"), new Reference(1)),
68 | };
69 |
70 | var filter = new UnusedReferencesTokenFilter();
71 | var newlist = filter.FilterUnusedReferences(nonCycleTokens);
72 |
73 | // test
74 | var expected = new List()
75 | {
76 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldA"), "value1", null, typeof(string)),
77 | new Token(TokenType.FieldnameWithTypeAndReference, new Field("fieldB"), "value2", new Reference(0), typeof(int)),
78 | Token.SeenBefore(new Field("FieldB"), new Reference(0)),
79 | };
80 |
81 | CollectionAssert.AreEqual(expected, newlist);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/PerformanceTests/PerformanceTestsBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Diagnostics;
22 | using NUnit.Framework;
23 |
24 | namespace StatePrinting.Tests.PerformanceTests
25 | {
26 | [TestFixture]
27 | public abstract class PerformanceTestsBase
28 | {
29 | [SetUp]
30 | public void Setup()
31 | {
32 | #if DEBUG
33 | throw new Exception("Only do performance in release mode");
34 | #endif
35 | }
36 |
37 | protected long Time(Action a)
38 | {
39 | var watch = new Stopwatch();
40 | watch.Start();
41 | a();
42 | watch.Stop();
43 | return watch.ElapsedMilliseconds;
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/StatePrinter.Tests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Reflection;
22 | using System.Runtime.InteropServices;
23 |
24 | // General Information about an assembly is controlled through the following
25 | // set of attributes. Change these attribute values to modify the information
26 | // associated with an assembly.
27 | [assembly: AssemblyTitle("StatePrinter.Tests")]
28 | [assembly: AssemblyDescription("Tests for the StatePrinter. Apache v2.0 License")]
29 | [assembly: AssemblyConfiguration("")]
30 | [assembly: AssemblyCompany("Kasper B. Graversen, Inc.")]
31 | [assembly: AssemblyProduct("StatePrinter.Tests")]
32 | [assembly: AssemblyCopyright("Kasper B. Graversen")]
33 | [assembly: AssemblyTrademark("")]
34 | [assembly: AssemblyCulture("")]
35 | [assembly: CLSCompliant(false)]
36 |
37 | // Setting ComVisible to false makes the types in this assembly not visible
38 | // to COM components. If you need to access a type in this assembly from
39 | // COM, set the ComVisible attribute to true on that type.
40 | [assembly: ComVisible(false)]
41 |
42 | // The following GUID is for the ID of the typelib if this project is exposed to COM
43 | [assembly: Guid("c1cd611d-2fe0-4f10-9202-72161591749a")]
44 |
45 | // Version information for an assembly consists of the following four values:
46 | //
47 | // Major Version
48 | // Minor Version
49 | // Build Number
50 | // Revision
51 | //
52 | // You can specify all the values or you can default the Build and Revision Numbers
53 | // by using the '*' as shown below:
54 | // [assembly: AssemblyVersion("1.0.*")]
55 | [assembly: AssemblyVersion("1.0.0.0")]
56 | [assembly: AssemblyFileVersion("1.0.0.0")]
57 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/TestHelper.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System.Globalization;
21 | using StatePrinting.Configurations;
22 | using StatePrinting.TestAssistance;
23 |
24 | namespace StatePrinting.Tests
25 | {
26 | static class TestHelper
27 | {
28 | public static Asserter Assert()
29 | {
30 | return CreateTestPrinter().Assert;
31 | }
32 |
33 | public static Asserter CreateShortAsserter()
34 | {
35 | return new Stateprinter(CreateTestConfiguration()).Assert;
36 | }
37 |
38 | public static Stateprinter CreateTestPrinter()
39 | {
40 | var cfg = ConfigurationHelper.GetStandardConfiguration()
41 | .SetCulture(CultureInfo.CreateSpecificCulture("da-DK"))
42 | .Test.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
43 | .Test.SetAutomaticTestRewrite(filename => new EnvironmentReader().UseTestAutoRewrite());
44 |
45 | return new Stateprinter(cfg);
46 | }
47 |
48 | public static Configuration CreateTestConfiguration()
49 | {
50 | var cfg = ConfigurationHelper
51 | .GetStandardConfiguration("")
52 | .SetNewlineDefinition(" ")
53 | .SetCulture(CultureInfo.CreateSpecificCulture("da-DK"))
54 | .Test.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
55 | .Test.SetAutomaticTestRewrite(filename => new EnvironmentReader().UseTestAutoRewrite());
56 |
57 | return cfg;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/TestingAssistance/EnvironmentReaderTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using NUnit.Framework;
22 | using StatePrinting.TestAssistance;
23 |
24 | namespace StatePrinting.Tests.TestingAssistance
25 | {
26 | [TestFixture]
27 | class EnvironmentReaderTest
28 | {
29 | [Test]
30 | public void TestReadUseAutoReWrite()
31 | {
32 | var org = Environment.GetEnvironmentVariable(EnvironmentReader.Usetestautorewrite, EnvironmentVariableTarget.User);
33 | try
34 | {
35 | Environment.SetEnvironmentVariable(EnvironmentReader.Usetestautorewrite, "false", EnvironmentVariableTarget.User);
36 |
37 | var reader = new EnvironmentReader();
38 | Assert.AreEqual(false, reader.UseTestAutoRewrite());
39 |
40 | Environment.SetEnvironmentVariable(EnvironmentReader.Usetestautorewrite, "true", EnvironmentVariableTarget.User);
41 | Assert.AreEqual(true, reader.UseTestAutoRewrite());
42 | }
43 | finally
44 | {
45 | Environment.SetEnvironmentVariable(EnvironmentReader.Usetestautorewrite, org, EnvironmentVariableTarget.User);
46 | }
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/TestingAssistance/ReWriterMockedTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 |
21 | using System;
22 | using NUnit.Framework;
23 | using StatePrinting.Tests.Mocks;
24 |
25 | namespace StatePrinting.Tests.TestingAssistance
26 | {
27 | public class UserStory_Integration_with_underlying_testing_framework
28 | {
29 | [Test]
30 | public void Rewriter_calls_to_testframework_autorewriting()
31 | {
32 | var printer = TestHelper.CreateTestPrinter();
33 |
34 | var fakeReadContent = new System.Text.UTF8Encoding(true).GetBytes(TestFileContent);
35 | var mock = new FileRepositoryMock(fakeReadContent);
36 | printer.Configuration.FactoryFileRepository = () => mock;
37 | printer.Configuration.Test.SetAutomaticTestRewrite(x => true);
38 |
39 | var assertMock = new AreEqualsMethodMock();
40 | printer.Configuration.Test.SetAreEqualsMethod(assertMock.AreEqualsMock);
41 |
42 | string expected = "boo";
43 | printer.Assert.AreAlike(expected, "actul");
44 |
45 | Assert.AreEqual("boo", assertMock.Expected);
46 | Assert.AreEqual("actul", assertMock.Actual);
47 | Assert.IsTrue(assertMock.Message.StartsWith("Rewritting test expectations in '"));
48 | Assert.IsTrue(assertMock.Message.EndsWith(@"'.
49 | Compile and re-run to see green lights.
50 | New expectations:
51 | ""actul"""));
52 | Assert.IsTrue(mock.WritePath.EndsWith("ReWriterMockedTests.cs"));
53 | }
54 |
55 | const string TestFileContent = @"
56 | must
57 | contain
58 | more
59 | lines than the
60 | test above
61 | such that the
62 | file contains as many lines as the line number
63 | reported by the callstackreflector
64 | 0
65 | 1
66 | 2
67 | 3
68 | 4
69 | 5
70 | 6
71 | 7
72 | 8
73 | 9
74 | 0
75 | 1
76 | 2
77 | 3
78 | 4
79 | 5
80 | 6
81 | 7
82 | 8
83 | 9
84 | 0
85 | 1
86 | 2
87 | 3
88 | 4
89 | 5
90 | 6
91 | 7
92 | 8
93 | 9
94 | string expected = @""boo"";
95 | 0
96 | 1
97 | 2
98 | 3
99 | 4
100 | 5
101 | ";
102 |
103 |
104 | ///
105 | /// By running again against the same file, the line number has now increased to something larger than the input file
106 | ///
107 | [Test]
108 | public void Rewriter_calls_to_testframework_fileTooShort()
109 | {
110 | var printer = TestHelper.CreateTestPrinter();
111 |
112 | var fakeReadContent = new System.Text.UTF8Encoding(true).GetBytes(TestFileContent);
113 | printer.Configuration.FactoryFileRepository = () => new FileRepositoryMock(fakeReadContent);
114 | printer.Configuration.Test.SetAutomaticTestRewrite((x) => true);
115 |
116 | var assertMock = new AreEqualsMethodMock();
117 | printer.Configuration.Test.SetAreEqualsMethod(assertMock.AreEqualsMock);
118 |
119 | string expected = @"expect";
120 |
121 | var ex = Assert.Throws(() => printer.Assert.AreAlike(expected, "actul"));
122 | Assert.AreEqual("File does not have 121 lines. Only 47 lines.\r\nParameter name: content", ex.Message);
123 | }
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/TestingAssistance/TestingAssistanceReWriteTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 |
22 | namespace StatePrinting.Tests.TestingAssistance
23 | {
24 |
25 | ///
26 | /// These tests are a bit weird, in that they autocorrect themselves. Ie. we cannot make them fail as they rewrite.
27 | ///
28 | /// used for manually testing that the rewrite still works.
29 | ///
30 | [TestFixture]
31 | class TestingAssistanceRewriteTest
32 | {
33 | [Test]
34 | [Explicit]
35 | public void Autocorrection_works_var()
36 | {
37 | var assert = TestHelper.Assert();
38 | assert.Configuration.Test.SetAutomaticTestRewrite((x) => true);
39 |
40 | var expected = @"""test auto""";
41 | assert.PrintAreAlike(expected, "test auto");
42 | }
43 |
44 | [Test]
45 | [Explicit]
46 | public void Autocorrection_works_string()
47 | {
48 | var assert = TestHelper.Assert();
49 | assert.Configuration.Test.SetAutomaticTestRewrite((x) => true);
50 |
51 | string expected = @"""test auto""";
52 | assert.PrintAreAlike(expected, "test auto");
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/TestingAssistance/USerStory.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 |
21 | using NUnit.Framework;
22 | using Is = StatePrinting.TestAssistance.Is;
23 |
24 | namespace StatePrinting.Tests.TestingAssistance
25 | {
26 | [TestFixture]
27 | class Userstory_nonexplicit
28 | {
29 | [Test]
30 | public void AreAlike_differentNewlines()
31 | {
32 | TestHelper.Assert().AreAlike("a\n", "a\r\n");
33 | TestHelper.Assert().AreAlike("a\r\n", "a\n");
34 | TestHelper.Assert().AreAlike("a\r", "a\n");
35 | TestHelper.Assert().AreAlike("a\r", "a\r\n");
36 |
37 | TestHelper.Assert().PrintAreAlike("\"a\r\"", "a\r\n");
38 | TestHelper.Assert().PrintAreAlike("\"a\r\"", "a\r");
39 | TestHelper.Assert().PrintEquals("\"a\r\"", "a\r");
40 | }
41 | }
42 |
43 | [Explicit("Run these in order to see how Nunit integrates with the testing assistance")]
44 | [TestFixture]
45 | class Userstory
46 | {
47 | [Test]
48 | public void AreEquals_without()
49 | {
50 | TestHelper.Assert().AreEqual("a", "b");
51 | }
52 |
53 | [Test]
54 | public void That_without()
55 | {
56 | TestHelper.Assert().That("a", Is.EqualTo("b"));
57 | }
58 |
59 | [Test]
60 | public void AreEquals_with()
61 | {
62 | TestHelper.Assert().AreEqual("a", "\"b\"");
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/ValueConverters/GenericValueConverterTest.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using NUnit.Framework;
21 | using StatePrinting.ValueConverters;
22 |
23 | namespace StatePrinting.Tests.ValueConverters
24 | {
25 | [TestFixture]
26 | class GenericValueConverterTest
27 | {
28 | const int anyInt = 42;
29 |
30 | [Test]
31 | public void TestConvertCallsLambda()
32 | {
33 | bool called = false;
34 | var sut = new GenericValueConverter(x=>"" + (called = true));
35 |
36 | sut.Convert(anyInt);
37 |
38 | Assert.IsTrue(called);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/StatePrinter.Tests/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/StatePrinter.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | StatePrinter
5 | 4.0.0
6 |
7 | An open source utility to turn object graphs into strings. Automate your ToString and unit tests asserts.
8 |
9 |
10 | * Requires .NET Framework 4.7.2
11 | * Added `TestingBehaviour.SetAreEqualsMethod()` for easier integration with NUnit v3.x
12 | * Removed all obsolete functionality.
13 |
14 | UnitTest ToString Serialization Approvals Test Testing ApprovalsTest
15 | Copyright 2014-2020
16 | en-US
17 | Kasper B. Graversen
18 | https://github.com/kbilsted/StatePrinter
19 | https://raw.githubusercontent.com/kbilsted/StatePrinter/master/StatePrinter/gfx/stateprinter.png
20 | false
21 |
22 |
23 |
--------------------------------------------------------------------------------
/StatePrinter.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29806.167
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePrinter", "StatePrinter\StatePrinter.csproj", "{10181C0E-31FF-48B3-A293-D9ED83D38C8D}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{653DACEF-B390-4AAB-87B8-CE18674319EE}"
9 | ProjectSection(SolutionItems) = preProject
10 | .nuget\NuGet.Config = .nuget\NuGet.Config
11 | .nuget\NuGet.exe = .nuget\NuGet.exe
12 | .nuget\NuGet.targets = .nuget\NuGet.targets
13 | EndProjectSection
14 | EndProject
15 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePrinter.Tests", "StatePrinter.Tests\StatePrinter.Tests.csproj", "{05FFACFD-A00F-4A47-9DDA-CAD57C2DDC59}"
16 | EndProject
17 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5DF8CA6E-4233-40D0-B3FF-F2FA145A351C}"
18 | ProjectSection(SolutionItems) = preProject
19 | appveyor.yml = appveyor.yml
20 | CHANGELOG.md = CHANGELOG.md
21 | CreateNuget.cmd = CreateNuget.cmd
22 | DeployToNuget.cmd = DeployToNuget.cmd
23 | StatePrinter.nuspec = StatePrinter.nuspec
24 | EndProjectSection
25 | EndProject
26 | Global
27 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
28 | Debug|Any CPU = Debug|Any CPU
29 | Release|Any CPU = Release|Any CPU
30 | EndGlobalSection
31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
32 | {10181C0E-31FF-48B3-A293-D9ED83D38C8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33 | {10181C0E-31FF-48B3-A293-D9ED83D38C8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
34 | {10181C0E-31FF-48B3-A293-D9ED83D38C8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
35 | {10181C0E-31FF-48B3-A293-D9ED83D38C8D}.Release|Any CPU.Build.0 = Release|Any CPU
36 | {05FFACFD-A00F-4A47-9DDA-CAD57C2DDC59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37 | {05FFACFD-A00F-4A47-9DDA-CAD57C2DDC59}.Debug|Any CPU.Build.0 = Debug|Any CPU
38 | {05FFACFD-A00F-4A47-9DDA-CAD57C2DDC59}.Release|Any CPU.ActiveCfg = Release|Any CPU
39 | {05FFACFD-A00F-4A47-9DDA-CAD57C2DDC59}.Release|Any CPU.Build.0 = Release|Any CPU
40 | EndGlobalSection
41 | GlobalSection(SolutionProperties) = preSolution
42 | HideSolutionNode = FALSE
43 | EndGlobalSection
44 | GlobalSection(ExtensibilityGlobals) = postSolution
45 | SolutionGuid = {4A127BF8-6D2D-444A-84C6-3CCD19F1AF58}
46 | EndGlobalSection
47 | EndGlobal
48 |
--------------------------------------------------------------------------------
/StatePrinter/Configurations/ConfigurationHelper.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using StatePrinting.FieldHarvesters;
22 | using StatePrinting.OutputFormatters;
23 | using StatePrinting.TestAssistance;
24 | using StatePrinting.ValueConverters;
25 |
26 | namespace StatePrinting.Configurations
27 | {
28 | ///
29 | /// Helper for configuration
30 | ///
31 | public static class ConfigurationHelper
32 | {
33 | ///
34 | /// Return a configuration which covers most usages.
35 | /// The configuration returned can be further remolded by adding additional handlers.
36 | ///
37 | /// Eg. add a to restrict the printed state to only public fields.
38 | ///
39 | public static Configuration GetStandardConfiguration(TestFrameworkAreEqualsMethod areEqualsMethod)
40 | {
41 | if (areEqualsMethod == null)
42 | throw new ArgumentNullException("areEqualsMethod");
43 |
44 | return GetStandardConfiguration(
45 | Configuration.DefaultIndention,
46 | areEqualsMethod: areEqualsMethod);
47 | }
48 |
49 | ///
50 | /// Return a configuration which covers most usages.
51 | /// The configuration returned can be further remolded by adding additional handlers.
52 | ///
53 | /// Eg. add a to restrict the printed state to only public fields.
54 | ///
55 | public static Configuration GetStandardConfiguration(
56 | string indentIncrement = Configuration.DefaultIndention,
57 | TestFrameworkAreEqualsMethod areEqualsMethod = null)
58 | {
59 | var cfg = new Configuration(indentIncrement, areEqualsMethod);
60 |
61 | // valueconverters
62 | cfg.Add(new StandardTypesConverter(cfg));
63 | cfg.Add(new StringConverter());
64 | cfg.Add(new DateTimeConverter(cfg));
65 | cfg.Add(new EnumConverter());
66 |
67 | // harvesters
68 | cfg.Add(new AllFieldsHarvester());
69 |
70 | // outputformatters
71 | cfg.OutputFormatter = new CurlyBraceStyle(cfg);
72 |
73 | return cfg;
74 | }
75 | }
76 | }
--------------------------------------------------------------------------------
/StatePrinter/Configurations/LegacyBehaviour.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | namespace StatePrinting.Configurations
21 | {
22 | public class LegacyBehaviour
23 | {
24 | ///
25 | /// To mimic the behaviour of v1.0.6 and below, set this to false.
26 | ///
27 | public bool TrimTrailingNewlines = true;
28 | }
29 | }
--------------------------------------------------------------------------------
/StatePrinter/Configurations/TestingBehaviour.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Data.SqlClient;
22 | using StatePrinting.TestAssistance;
23 |
24 | namespace StatePrinting.Configurations
25 | {
26 | public class TestingBehaviour
27 | {
28 | readonly Configuration configuration;
29 |
30 | public TestingBehaviour(Configuration configuration)
31 | {
32 | this.configuration = configuration;
33 | }
34 |
35 | ///
36 | /// Configure how to call AreEquals in the unit testing framework of your choice.
37 | /// Only set this field if you are using the functionality.
38 | ///
39 | public Configuration SetAreEqualsMethod(TestFrameworkAreEqualsMethod areEqualsMethod)
40 | {
41 | if (areEqualsMethod == null)
42 | throw new ArgumentNullException("areEqualsMethod");
43 | AreEqualsMethod = areEqualsMethod;
44 |
45 | return configuration;
46 | }
47 |
48 | private static readonly object[] NoArg = new object[0];
49 |
50 | ///
51 | /// Configure how to call AreEquals in the unit testing framework of your choice.
52 | /// Only set this field if you are using the functionality.
53 | /// This overload is for easy integration with NUnit 3.x Asert.AreEqual
54 | ///
55 | public Configuration SetAreEqualsMethod(Action nunitAreEqualsMethod)
56 | {
57 | if (nunitAreEqualsMethod == null)
58 | throw new ArgumentNullException("nunitAreEqualsMethod");
59 | AreEqualsMethod = new TestFrameworkAreEqualsMethod((actual, expected, message) => nunitAreEqualsMethod(actual,expected,message,NoArg));
60 |
61 | return configuration;
62 | }
63 |
64 | ///
65 | /// Configure how to call AreEquals in the unit testing framework of your choice.
66 | /// Only set this field if you are using the functionality.
67 | ///
68 | public TestFrameworkAreEqualsMethod AreEqualsMethod { get; private set; }
69 |
70 | ///
71 | /// The signature for finding out if a test's expected value may be automatically re-written.
72 | ///
73 | /// True if the test may be rewritten with the new expected value to make the test pass again.
74 | public delegate bool TestRewriteIndicator(UnitTestLocationInfo location);
75 |
76 | ///
77 | /// Evaluate the function for each failing test.
78 | /// Your function can rely on anything such as an environment variable or a file on the file system.
79 | /// If you only want to do this evaluation once pr. test suite execution you should wrap your function in a Lazy<>
80 | ///
81 | public TestRewriteIndicator AutomaticTestRewrite { get; private set; }
82 |
83 | ///
84 | /// Evaluate the function for each failing test.
85 | /// Your function can rely on anything such as an environment variable or a file on the file system.
86 | /// If you only want to do this evaluation once pr. test suite execution you should wrap your function in a Lazy<>
87 | ///
88 | public Configuration SetAutomaticTestRewrite(TestRewriteIndicator indicator)
89 | {
90 | if (indicator == null)
91 | throw new ArgumentNullException("indicator");
92 | AutomaticTestRewrite = indicator;
93 |
94 | return configuration;
95 | }
96 |
97 | ///
98 | /// Defines how the error message shown when tests are failing
99 | ///
100 | public delegate string CreateAssertMessageCallback(
101 | string expected,
102 | string actual,
103 | string escapedActual,
104 | bool willPerformAutomaticRewrite,
105 | UnitTestLocationInfo location);
106 |
107 | public CreateAssertMessageCallback AssertMessageCreator { get; private set; }
108 |
109 | ///
110 | /// Set the method to be called when an assert message is to be created
111 | ///
112 | public Configuration SetAssertMessageCreator(CreateAssertMessageCallback assertMessageCreator)
113 | {
114 | if (assertMessageCreator == null)
115 | throw new ArgumentNullException("indicator");
116 | AssertMessageCreator = assertMessageCreator;
117 |
118 | return configuration;
119 | }
120 | }
121 | }
--------------------------------------------------------------------------------
/StatePrinter/FieldHarvesters/AllFieldsAndPropertiesHarvester.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace StatePrinting.FieldHarvesters
24 | {
25 | ///
26 | /// Harvest all fields, public and private.
27 | ///
28 | /// We ignore the types from the following namespaces
29 | ///
30 | ///
31 | ///
32 | ///
33 | public class AllFieldsAndPropertiesHarvester : IFieldHarvester
34 | {
35 | readonly HarvestHelper harvestHelper = new HarvestHelper();
36 |
37 | public bool CanHandleType(Type type)
38 | {
39 | return true;
40 | }
41 |
42 | ///
43 | /// We ignore all properties as they, in the end, will only point to some computed state or other fields.
44 | /// Hence they do not provide information about the actual state of the object.
45 | ///
46 | public List GetFields(Type type)
47 | {
48 | return harvestHelper.GetFieldsAndProperties(type);
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/StatePrinter/FieldHarvesters/AllFieldsHarvester.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace StatePrinting.FieldHarvesters
24 | {
25 | ///
26 | /// Harvest all fields, public and private.
27 | ///
28 | /// We ignore the types from the following namespaces
29 | ///
30 | ///
31 | ///
32 | ///
33 | public class AllFieldsHarvester : IFieldHarvester
34 | {
35 | readonly HarvestHelper harvestHelper = new HarvestHelper();
36 | public bool CanHandleType(Type type)
37 | {
38 | return true;
39 | }
40 |
41 | ///
42 | /// We ignore all properties as they, in the end, will only point to some computed state or other fields.
43 | /// Hence they do not provide information about the actual state of the object.
44 | ///
45 | public List GetFields(Type type)
46 | {
47 | return harvestHelper.GetFields(type);
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/StatePrinter/FieldHarvesters/AnonymousFieldHarvester.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace StatePrinting.FieldHarvesters
24 | {
25 | ///
26 | /// Harvest from a type as specified by the provided functions.
27 | ///
28 | public class AnonymousHarvester : IFieldHarvester
29 | {
30 | private readonly Func canHandleTypeFunc;
31 | private readonly Func> getFieldsFunc;
32 |
33 | public AnonymousHarvester(Func canHandleTypeFunc, Func> getFieldsFunc)
34 | {
35 | this.canHandleTypeFunc = canHandleTypeFunc;
36 | this.getFieldsFunc = getFieldsFunc;
37 | }
38 |
39 | public bool CanHandleType(Type type)
40 | {
41 | return canHandleTypeFunc(type);
42 | }
43 |
44 | public List GetFields(Type type)
45 | {
46 | return getFieldsFunc(type);
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/StatePrinter/FieldHarvesters/IFieldHarvester.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Collections.Generic;
22 |
23 | namespace StatePrinting.FieldHarvesters
24 | {
25 | ///
26 | /// A fieldharvester is a configuration part that given a type is able to harvest all fields on it.
27 | ///
28 | public interface IFieldHarvester
29 | {
30 | bool CanHandleType(Type type);
31 |
32 | List GetFields(Type type);
33 | }
34 | }
--------------------------------------------------------------------------------
/StatePrinter/FieldHarvesters/IRunTimeCodeGenerator.cs:
--------------------------------------------------------------------------------
1 | // Copyright 2014-2020 Kasper B. Graversen
2 | //
3 | // Licensed to the Apache Software Foundation (ASF) under one
4 | // or more contributor license agreements. See the NOTICE file
5 | // distributed with this work for additional information
6 | // regarding copyright ownership. The ASF licenses this file
7 | // to you under the Apache License, Version 2.0 (the
8 | // "License"); you may not use this file except in compliance
9 | // with the License. You may obtain a copy of the License at
10 | //
11 | // http://www.apache.org/licenses/LICENSE-2.0
12 | //
13 | // Unless required by applicable law or agreed to in writing,
14 | // software distributed under the License is distributed on an
15 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | // KIND, either express or implied. See the License for the
17 | // specific language governing permissions and limitations
18 | // under the License.
19 |
20 | using System;
21 | using System.Reflection;
22 |
23 | namespace StatePrinting.FieldHarvesters
24 | {
25 | public interface IRunTimeCodeGenerator
26 | {
27 | Func