Tests and package name updated
This commit is contained in:
8
Tests/Editor.meta
Normal file
8
Tests/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 98341f79b8169426c8edbc4469715037
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Tests/Editor/Darkmatter.FonepayUnity.Editor.Tests.asmdef
Normal file
25
Tests/Editor/Darkmatter.FonepayUnity.Editor.Tests.asmdef
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "Darkmatter.FonepayUnity.Editor.Tests",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Darkmatter.FonepayUnity",
|
||||
"Darkmatter.FonepayUnity.Editor",
|
||||
"UnityEditor.TestRunner",
|
||||
"UnityEngine.TestRunner"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a4ef70a61be54ea6b8ada95c5e2743c
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
78
Tests/Editor/WebsocketMessageTests.cs
Normal file
78
Tests/Editor/WebsocketMessageTests.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Darkmatter.Fonepay;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Darkmatter.FonepayUnity.Editor.Tests
|
||||
{
|
||||
public class WebsocketMessageTests
|
||||
{
|
||||
[Test]
|
||||
public void Status_ParsesNestedJsonString()
|
||||
{
|
||||
const string raw =
|
||||
"{\"merchantId\":\"M1\",\"deviceId\":\"D1\"," +
|
||||
"\"transactionStatus\":\"{\\\"paymentSuccess\\\":true,\\\"success\\\":true,\\\"amount\\\":42.5}\"}";
|
||||
|
||||
var envelope = JsonUtility.FromJson<WebsocketMessage<QRPaymentStatus>>(raw);
|
||||
var status = envelope.Status;
|
||||
|
||||
Assert.IsTrue(status.success);
|
||||
Assert.IsTrue(status.paymentSuccess);
|
||||
Assert.AreEqual(42.5f, status.amount);
|
||||
Assert.AreEqual(PaymentOutcome.Complete, status.Outcome);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Status_QrVerifiedFrame()
|
||||
{
|
||||
const string raw =
|
||||
"{\"transactionStatus\":\"{\\\"success\\\":true,\\\"qrVerified\\\":true,\\\"message\\\":\\\"ok\\\"}\"}";
|
||||
|
||||
var envelope = JsonUtility.FromJson<WebsocketMessage<QRVerificationStatus>>(raw);
|
||||
var v = envelope.Status;
|
||||
|
||||
Assert.IsTrue(v.qrVerified);
|
||||
Assert.AreEqual("ok", v.message);
|
||||
}
|
||||
}
|
||||
|
||||
public class PaymentOutcomeTests
|
||||
{
|
||||
[Test]
|
||||
public void SuccessTrue_PaymentTrue_Complete()
|
||||
{
|
||||
var s = new QRPaymentStatus { success = true, paymentSuccess = true };
|
||||
Assert.AreEqual(PaymentOutcome.Complete, s.Outcome);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuccessTrue_PaymentFalse_CancelledByUser()
|
||||
{
|
||||
var s = new QRPaymentStatus { success = true, paymentSuccess = false };
|
||||
Assert.AreEqual(PaymentOutcome.CancelledByUser, s.Outcome);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SuccessFalse_Failed()
|
||||
{
|
||||
var s = new QRPaymentStatus { success = false, paymentSuccess = true };
|
||||
Assert.AreEqual(PaymentOutcome.Failed, s.Outcome);
|
||||
|
||||
s = new QRPaymentStatus { success = false, paymentSuccess = false };
|
||||
Assert.AreEqual(PaymentOutcome.Failed, s.Outcome);
|
||||
}
|
||||
}
|
||||
|
||||
public class FonepayErrorTests
|
||||
{
|
||||
[Test]
|
||||
public void Carries_CodeAndDocs()
|
||||
{
|
||||
var err = new FonepayError(42, "boom", "docs/url");
|
||||
Assert.AreEqual(42, err.ErrorCode);
|
||||
Assert.AreEqual("docs/url", err.Docs);
|
||||
Assert.AreEqual("boom", err.Message);
|
||||
StringAssert.Contains("ErrorCode: 42", err.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Tests/Editor/WebsocketMessageTests.cs.meta
Normal file
2
Tests/Editor/WebsocketMessageTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b507c7c2bcf2f403fb4157318b8d7e97
|
||||
8
Tests/Runtime.meta
Normal file
8
Tests/Runtime.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6639ec7313a77493fb517eb322745684
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
21
Tests/Runtime/Darkmatter.FonepayUnity.Runtime.Tests.asmdef
Normal file
21
Tests/Runtime/Darkmatter.FonepayUnity.Runtime.Tests.asmdef
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "Darkmatter.FonepayUnity.Runtime.Tests",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Darkmatter.FonepayUnity",
|
||||
"UnityEngine.TestRunner"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"nunit.framework.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [
|
||||
"UNITY_INCLUDE_TESTS"
|
||||
],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b7c0851a1002412dadc7d1c37a4cef5
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
100
Tests/Runtime/FonepayRuntimeTests.cs
Normal file
100
Tests/Runtime/FonepayRuntimeTests.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Darkmatter.Fonepay;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
namespace Darkmatter.FonepayUnity.Tests
|
||||
{
|
||||
public class WebsocketMessagePlayModeTests
|
||||
{
|
||||
[Test]
|
||||
public void Status_ParsesNestedJsonInPlayer()
|
||||
{
|
||||
const string raw =
|
||||
"{\"transactionStatus\":\"{\\\"paymentSuccess\\\":true,\\\"success\\\":true,\\\"amount\\\":7}\"}";
|
||||
|
||||
var envelope = JsonUtility.FromJson<WebsocketMessage<QRPaymentStatus>>(raw);
|
||||
var status = envelope.Status;
|
||||
|
||||
Assert.IsTrue(status.paymentSuccess);
|
||||
Assert.AreEqual(7f, status.amount);
|
||||
Assert.AreEqual(PaymentOutcome.Complete, status.Outcome);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Status_ReparsesEachAccess()
|
||||
{
|
||||
const string raw =
|
||||
"{\"transactionStatus\":\"{\\\"success\\\":true,\\\"paymentSuccess\\\":true}\"}";
|
||||
|
||||
var envelope = JsonUtility.FromJson<WebsocketMessage<QRPaymentStatus>>(raw);
|
||||
var a = envelope.Status;
|
||||
var b = envelope.Status;
|
||||
|
||||
Assert.AreEqual(a.Outcome, b.Outcome);
|
||||
Assert.AreEqual(PaymentOutcome.Complete, a.Outcome);
|
||||
}
|
||||
}
|
||||
|
||||
public class FonepayClientPlayModeTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Reset() => FonepayConfig.Invalidate();
|
||||
|
||||
[Test]
|
||||
public void Ctor_WithoutConfigAsset_Throws()
|
||||
{
|
||||
Assert.Throws<FonepayError>(() => new FonepayClient());
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AwaitPaymentAsync_EmptyUrl_Throws() => RunAsync(async () =>
|
||||
{
|
||||
var client = TryBuildClient();
|
||||
if (client == null)
|
||||
{
|
||||
Assert.Pass("Skipped: no FonepayConfig in test context.");
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.ThrowsAsync<ArgumentException>(async () =>
|
||||
await client.AwaitPaymentAsync(string.Empty));
|
||||
await Task.CompletedTask;
|
||||
});
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AwaitPaymentAsync_PreCancelled_Throws() => RunAsync(async () =>
|
||||
{
|
||||
var client = TryBuildClient();
|
||||
if (client == null)
|
||||
{
|
||||
Assert.Pass("Skipped: no FonepayConfig in test context.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var cts = new CancellationTokenSource();
|
||||
cts.Cancel();
|
||||
|
||||
Assert.ThrowsAsync<OperationCanceledException>(async () =>
|
||||
await client.AwaitPaymentAsync("ws://127.0.0.1:1/", ct: cts.Token));
|
||||
await Task.CompletedTask;
|
||||
});
|
||||
|
||||
private static FonepayClient TryBuildClient()
|
||||
{
|
||||
try { return new FonepayClient(); }
|
||||
catch (FonepayError) { return null; }
|
||||
}
|
||||
|
||||
private static IEnumerator RunAsync(Func<Task> body)
|
||||
{
|
||||
var t = body();
|
||||
while (!t.IsCompleted) yield return null;
|
||||
if (t.IsFaulted) throw t.Exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Tests/Runtime/FonepayRuntimeTests.cs.meta
Normal file
2
Tests/Runtime/FonepayRuntimeTests.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3eeb964c506324b58bd6f95c088096fa
|
||||
Reference in New Issue
Block a user