Skip to content

Commit

Permalink
simplify tests (#97)
Browse files Browse the repository at this point in the history
Add unit tests, to simplify test execution because are compiler inside the test project, not in an external project like the integration tests

Use test assets from protobuff repo

Convert `package directive` integration test to unit test
  • Loading branch information
enricosada authored and Dave Thomas committed Jan 2, 2019
1 parent 547a09f commit 267e7c5
Show file tree
Hide file tree
Showing 14 changed files with 570 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,4 @@ paket-files/
target/
/example/bundle.fs
.store
*.proto.fs
4 changes: 3 additions & 1 deletion build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</Target>

<Target Name="Build" DependsOnTargets="Restore">
<Exec Command='dotnet build Falanx.Generator -c Release' />
<Exec Command='dotnet build Falanx.Tool -c Release' />
</Target>

<Target Name="Pack" DependsOnTargets="Restore">
Expand All @@ -23,6 +23,8 @@
</Target>

<Target Name="Test">
<Exec Command='dotnet build test/Falanx.Tests -c Release' />
<Exec Command='dotnet run -c Release -- --fail-on-focused-tests --summary' WorkingDirectory="$(RepoRootDir)/test/Falanx.Tests" IgnoreStandardErrorWarningFormat="true" />
<Exec Command='dotnet build test/Falanx.IntegrationTests' />
<Exec Command='dotnet run -- "$(Version)" --fail-on-focused-tests --summary' WorkingDirectory="$(RepoRootDir)/test/Falanx.IntegrationTests" IgnoreStandardErrorWarningFormat="true" />
</Target>
Expand Down
7 changes: 6 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ source https://www.nuget.org/api/v2
frameworks: netcoreapp2.0, netstandard2.0
storage: none

clitool dotnet-mergenupkg ~> 2.4
clitool dotnet-mergenupkg ~> 2.4

group ProtoTests

github protocolbuffers/protobuf:47d33e752e5256bec315e5867b940ca33d66e8a2 csharp/protos/unittest_proto3.proto
github protocolbuffers/protobuf:47d33e752e5256bec315e5867b940ca33d66e8a2 csharp/protos/unittest_import_proto3.proto
7 changes: 7 additions & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,10 @@ NUGET
Microsoft.NETCore.App (>= 2.0) - restriction: || (== netcoreapp2.0) (&& (== netstandard2.0) (>= netcoreapp2.0))
FSharp.Core (4.5.2) - restriction: || (== netcoreapp2.0) (&& (== netstandard2.0) (>= netcoreapp1.0))
Microsoft.NETCore.App (2.1.5) - restriction: || (== netcoreapp2.0) (&& (== netstandard2.0) (>= netcoreapp2.0))

GROUP ProtoTests

GITHUB
remote: protocolbuffers/protobuf
csharp/protos/unittest_import_proto3.proto (47d33e752e5256bec315e5867b940ca33d66e8a2)
csharp/protos/unittest_proto3.proto (47d33e752e5256bec315e5867b940ca33d66e8a2)
7 changes: 0 additions & 7 deletions test/Falanx.IntegrationTests/Sample.fs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,6 @@ let tests pkgUnderTestVersion =
|> buildExampleWithTemplate fs ``template3 binary+json`` ``sample6 bundle``
)

testCase |> withLog "can build sample5 pkg" (fun _ fs ->
let testDir = inDir fs "sanity_check_sample5"

testDir
|> buildExampleWithTemplate fs ``template1 binary`` ``sample5 pkg``
)

testCase |> withLog "can build sample7 json" (fun _ fs ->
let testDir = inDir fs "sanity_check_sample7_json"

Expand Down
5 changes: 0 additions & 5 deletions test/Falanx.IntegrationTests/TestAssets.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ type TestAssetExampleInfo =
FileNames: (Language * FormatKind * string) list
ProtoFile: string }

let ``sample5 pkg`` =
{ ExampleDir = "sample5 pkg"
FileNames = [FSharp, Binary, "BinaryExample.fs"]
ProtoFile = "bundle.proto" }

let ``sample6 bundle`` =
{ ExampleDir = "sample6 bundle"
FileNames = [FSharp, Binary, "BinaryExample.fs"; FSharp, Json, "JsonExample.fs"]
Expand Down
31 changes: 31 additions & 0 deletions test/Falanx.Tests/Compatibility.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module GoogleProtobufTest.V3.CompatibilityTests

// based on https://github.com/protocolbuffers/protobuf/blob/d9ccd0c0e6bbda9bf4476088eeb46b02d7dcd327/csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/JsonParserTest.cs

open System
open System.IO
open Expecto
open Expecto.Logging
open Expecto.Logging.Message
open Fleece
open Newtonsoft
open protobuf_unittest3

let jsonTests =
testList "json" [
testCase "Original field name accepted" (fun () ->
let json = """{ "singleInt32": 10 }"""
match parseJson json with
| Result.Ok msg ->
Expect.equal (Some(10)) msg.singleInt32 (sprintf "Failed to deserialize singleInt32, msg was %A" msg)
Expect.equal None msg.singleInt64 (sprintf "Expected default 0 on singleInt64, msg was %A" msg)
Expect.equal None msg.singleString (sprintf "Expected default empty string, msg was %A" msg)
| Result.Error err ->
failwithf "Failed to parse json: %A" err
)
]

let tests =
testList "compatibility" [
jsonTests
]
52 changes: 52 additions & 0 deletions test/Falanx.Tests/Falanx.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProtoFile Include="schemas\falanx_test.proto" />
<ProtoFile Include="schemas\bundle_with_package.proto" />
</ItemGroup>
<ItemGroup>
<Compile Include="Compatibility.fs" />
<Compile Include="Tests.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<None Include="paket.references" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Falanx.Proto.Codec.Binary\Falanx.Proto.Codec.Binary.fsproj" />
<ProjectReference Include="..\..\Falanx.Proto.Codec.Json\Falanx.Proto.Codec.Json.fsproj" />
</ItemGroup>
<!-- use the falanx generator binary directly -->
<PropertyGroup>
<FalanxSdk_GeneratorExeHost>dotnet </FalanxSdk_GeneratorExeHost>
<FalanxSdk_GeneratorExe>..\..\Falanx.Tool\bin\$(Configuration)\netcoreapp2.1\falanx.dll</FalanxSdk_GeneratorExe>
</PropertyGroup>
<!-- include to enable the generator for these formats -->
<Import Project="..\..\Falanx.Proto.Codec.Binary\build\Falanx.Proto.Codec.Binary.props" />
<Import Project="..\..\Falanx.Proto.Codec.Json\build\Falanx.Proto.Codec.Json.props" />
<!-- do import directly the falanx sdk target, instead from package -->
<Target Name="FalanxSdkGenerateCode" BeforeTargets="BeforeCompile" Inputs="@(ProtoFile)" Outputs="%(Identity).fs">
<PropertyGroup>
<_FalanxOutPath>%(ProtoFile.OutputPath)</_FalanxOutPath>
<_FalanxOutPath Condition=" '$(_FalanxOutPath)' == '' ">%(ProtoFile.Identity).fs</_FalanxOutPath>
</PropertyGroup>
<ItemGroup>
<FalanxSdk_CodeGenArgs Include="--inputfile &quot;%(ProtoFile.Identity)&quot;" />
<FalanxSdk_CodeGenArgs Include="--outputfile &quot;$(_FalanxOutPath)&quot;" />
<FalanxSdk_CodeGenArgs Include="--defaultnamespace &quot;$(RootNamespace)&quot;" />
<FalanxSdk_CodeGenArgs Include="--serializer %(FalanxSdkSerializer.Identity)" />
</ItemGroup>
<!-- Use dotnet to execute the process. -->
<Exec Command="$(FalanxSdk_GeneratorExeHost)&quot;$(FalanxSdk_GeneratorExe)&quot; @(FalanxSdk_CodeGenArgs -&gt; '%(Identity)', ' ')"
WorkingDirectory="$(MSBuildThisFileDirectory)" />
<ItemGroup>
<CompileBefore Include="$(_FalanxOutPath)" />
<FileWrites Include="$(_FalanxOutPath)" />
</ItemGroup>
</Target>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
24 changes: 24 additions & 0 deletions test/Falanx.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Falanx.Program

open Expecto
open System
open System.IO

let tests () =
[ GoogleProtobufTest.V3.CompatibilityTests.tests
Falanx.Tests.tests ]
|> testList "unit"
|> testSequenced

[<EntryPoint>]
let main argv =
let artifactsDir =
IO.Path.Combine(__SOURCE_DIRECTORY__,"..","..","artifact")
|> Path.GetFullPath

let resultsPath = IO.Path.Combine(artifactsDir,"test_results","TestResults.Unit.xml")

let writeResults = TestResults.writeNUnitSummary (resultsPath, "Falanx.Tests")
let config = defaultConfig.appendSummaryHandler writeResults

Tests.runTestsWithArgs config argv (tests ())
27 changes: 27 additions & 0 deletions test/Falanx.Tests/Tests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Falanx.Tests

open System
open System.IO
open Expecto
open Expecto.Logging
open Expecto.Logging.Message
open Fleece
open Newtonsoft

let jsonTests =
testList "json" [
testCase "package directive" (fun () ->
let json = """{ "martId": 10, "memberId": "a" }"""
match parseJson json with
| Result.Ok (msg: Sample.Tests.WithPackage.BundleRequest) ->
Expect.equal (Some(10)) msg.martId (sprintf "Failed to deserialize martId, msg was %A" msg)
Expect.equal (Some "a") msg.memberId (sprintf "Failed to deserialize memberId, msg was %A" msg)
| Result.Error err ->
failwithf "Failed to parse json: %A" err
)
]

let tests =
testList "general" [
jsonTests
]
5 changes: 5 additions & 0 deletions test/Falanx.Tests/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
group IntegrationTests

FSharp.Core
Expecto
Expecto.TestResults
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
syntax = "proto3";

package Sample.Tests;
package Sample.Tests.WithPackage;

message BundleRequest {
int32 martId = 1;
string member_id = 2;
string channel_type = 3;
repeated string retail_skus = 4;

}
Loading

0 comments on commit 267e7c5

Please sign in to comment.