Skip to content

Commit

Permalink
Helix Tests (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder authored Jun 13, 2024
1 parent 960815d commit 54e4c0d
Show file tree
Hide file tree
Showing 15 changed files with 603 additions and 47 deletions.
89 changes: 62 additions & 27 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = true

# Expression-bodied members
csharp_style_expression_bodied_accessors = true
csharp_style_expression_bodied_constructors = false
csharp_style_expression_bodied_indexers = true
csharp_style_expression_bodied_lambdas = true
csharp_style_expression_bodied_local_functions = when_on_single_line
csharp_style_expression_bodied_methods = when_on_single_line
csharp_style_expression_bodied_operators = when_on_single_line
csharp_style_expression_bodied_properties = when_on_single_line
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = when_on_single_line:silent
csharp_style_expression_bodied_methods = when_on_single_line:silent
csharp_style_expression_bodied_operators = when_on_single_line:silent
csharp_style_expression_bodied_properties = when_on_single_line:silent

# Pattern matching preferences
csharp_style_pattern_matching_over_as_with_null_check = true
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:silent
csharp_style_prefer_extended_property_pattern = true
csharp_style_prefer_not_pattern = true:warning
csharp_style_prefer_pattern_matching = true:suggestion
csharp_style_prefer_switch_expression = true
csharp_style_prefer_switch_expression = true:suggestion

# Null-checking preferences
csharp_style_conditional_delegate_call = true:warning
Expand All @@ -107,36 +107,36 @@ csharp_preferred_modifier_order = public,private,protected,internal,file,static,
csharp_style_prefer_readonly_struct = true:silent

# Code-block preferences
csharp_prefer_braces = when_multiline
csharp_prefer_simple_using_statement = true
csharp_prefer_braces = when_multiline:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = file_scoped:warning
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_top_level_statements = false
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = false:silent

# Expression-level preferences
csharp_prefer_simple_default_expression = true
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:warning
csharp_style_implicit_object_creation_when_type_is_apparent = true
csharp_style_inlined_variable_declaration = true
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_prefer_index_operator = true:silent
csharp_style_prefer_local_over_anonymous_function = true
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:warning
csharp_style_prefer_range_operator = true
csharp_style_prefer_tuple_swap = true
csharp_style_prefer_utf8_string_literals = true
csharp_style_prefer_range_operator = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_throw_expression = true:silent
csharp_style_unused_value_assignment_preference = discard_variable:warning
csharp_style_unused_value_assignment_preference = discard_variable:silent
csharp_style_unused_value_expression_statement_preference = discard_variable:warning

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:warning

# New line preferences
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false
csharp_style_allow_embedded_statements_on_same_line_experimental = false
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = false:silent
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = false:silent
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = false:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false:silent
csharp_style_allow_embedded_statements_on_same_line_experimental = false:silent

#### C# Formatting Rules ####

Expand Down Expand Up @@ -247,3 +247,38 @@ dotnet_diagnostic.S1075.severity = suggestion

# S1450: Private fields only used as local variables in methods should become local variables
dotnet_diagnostic.S1450.severity = suggestion
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion

[*.{cs,vb}]
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
dotnet_style_coalesce_expression = true:warning
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:warning
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = false:silent
dotnet_style_explicit_tuple_names = true:warning
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_readonly_field = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
dotnet_code_quality_unused_parameters = non_public:silent
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:warning
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:warning
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
30 changes: 30 additions & 0 deletions .github/workflows/test-helix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test (Helix)

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:

build:

runs-on: windows-latest

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Install .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Run tests
run: |
cd .\MiniTwitch.Helix.Test\
dotnet test
105 changes: 105 additions & 0 deletions MiniTwitch.Helix.Test/Converters/ConduitTransportConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System.Text.Json;
using MiniTwitch.Helix.Internal;
using MiniTwitch.Helix.Internal.Json;
using MiniTwitch.Helix.Responses;

namespace MiniTwitch.Helix.Test.Converters;

public class ConduitTransportConverterTest
{
static JsonSerializerOptions options = HelixApiClient.SerializerOptions;

[Fact]
public void Convert_Webhook()
{
string json = """
{
"id": "0",
"status": "enabled",
"transport": {
"method": "webhook",
"callback": "https://this-is-a-callback.com"
}
}
""";

var element = JsonDocument.Parse(json).RootElement.GetProperty("transport");
var res = ConduitTransportConverter.ReadTransport(element, options);
Assert.NotNull(res);
var t = Assert.IsType<ConduitTransport.Webhook>(res);
Assert.Equal("https://this-is-a-callback.com", t.Callback);
}

[Fact]
public void Convert_WebSocket_Alive()
{
string json = """
{
"id": "2",
"status": "enabled",
"transport": {
"method": "websocket",
"session_id": "9fd5164a-a958-4c60-b7f4-6a7202506ca0",
"connected_at": "2020-11-10T14:32:18.730260295Z"
}
}
""";

var element = JsonDocument.Parse(json).RootElement.GetProperty("transport");
var res = ConduitTransportConverter.ReadTransport(element, options);
Assert.NotNull(res);
var t = Assert.IsType<ConduitTransport.WebSocket>(res);
Assert.Equal("9fd5164a-a958-4c60-b7f4-6a7202506ca0", t.SessionId);
// DateTime parsing floors 730260295 -> 7302602
Assert.Equal(DateTime.Parse("2020-11-10T14:32:18.7302602Z").ToUniversalTime(), t.ConnectedAt);
Assert.Null(t.DisconnectedAt);
}

[Fact]
public void Convert_WebSocket_Dead()
{
string json = """
{
"id": "4",
"status": "websocket_disconnected",
"transport": {
"method": "websocket",
"session_id": "ad1c9fc3-0d99-4eb7-8a04-8608e8ff9ec9",
"connected_at": "2020-11-10T14:32:18.730260295Z",
"disconnected_at": "2020-11-11T14:32:18.730260295Z"
}
}
""";

var element = JsonDocument.Parse(json).RootElement.GetProperty("transport");
var res = ConduitTransportConverter.ReadTransport(element, options);
Assert.NotNull(res);
var t = Assert.IsType<ConduitTransport.WebSocket>(res);
Assert.Equal("ad1c9fc3-0d99-4eb7-8a04-8608e8ff9ec9", t.SessionId);
// DateTime parsing floors 730260295 -> 7302602
Assert.Equal(DateTime.Parse("2020-11-10T14:32:18.7302602Z").ToUniversalTime(), t.ConnectedAt);
Assert.Equal(DateTime.Parse("2020-11-11T14:32:18.7302602Z").ToUniversalTime(), t.DisconnectedAt);
}

[Fact]
public void Convert_Invalid()
{
string json = """
{
"id": "2",
"status": "enabled",
"transport": {
"method": "melon095",
"session_id": "9fd5164a-a958-4c60-b7f4-6a7202506ca0",
"connected_at": "2020-11-10T14:32:18.730260295Z"
}
}
""";

var element = JsonDocument.Parse(json).RootElement.GetProperty("transport");
var ex = Assert.Throws<JsonException>(() =>
{
var res = ConduitTransportConverter.ReadTransport(element, options);
});
}
}
44 changes: 44 additions & 0 deletions MiniTwitch.Helix.Test/Converters/EnumConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using MiniTwitch.Helix.Internal.Json;

namespace MiniTwitch.Helix.Test.Converters;

public class EnumConverterTest
{
[Fact]
public void Convert()
{
var e = EnumConverter<TestEnum>.ReadEnum("Forsaan");
Assert.Equal(TestEnum.Forsaan, e);
}

[Fact]
public void Convert_From_SnakeCase()
{
var e = EnumConverter<TestEnum>.ReadEnum("i_eat_rocks");
Assert.Equal(TestEnum.IEatRocks, e);
}

[Fact]
public void Convert_From_Null()
{
var e = EnumConverter<TestEnum>.ReadEnum(null);
Assert.Equal(default(TestEnum), e);
}

[Fact]
public void Convert_From_Undefined()
{
var e = EnumConverter<TestEnum>.ReadEnum("my life is doge");
Assert.Equal(default(TestEnum), e);
}

enum TestEnum
{
Unknown,
Forshon,
Forsen,
Forsaan,
IEatRocks,
XD
}
}
42 changes: 42 additions & 0 deletions MiniTwitch.Helix.Test/Converters/IntConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using MiniTwitch.Helix.Internal.Json;

namespace MiniTwitch.Helix.Test.Converters;

public class IntConverterTest
{
[Fact]
public void Read_Int()
{
var r = IntConverter.ReadInt(Encoding.UTF8.GetBytes("12345"));
Assert.Equal(12345, r);
}

[Fact]
public void Read_Long()
{
var r = IntConverter.ReadInt(Encoding.UTF8.GetBytes("8223372036854780000"));
Assert.Equal(0, r);
}

[Fact]
public void Read_Empty()
{
var r = IntConverter.ReadInt(Span<byte>.Empty);
Assert.Equal(0, r);
}

[Fact]
public void Read_Null()
{
var r2 = IntConverter.ReadInt(Encoding.UTF8.GetBytes("null"));
Assert.Equal(0, r2);
}

[Fact]
public void Read_Undefined()
{
var r2 = IntConverter.ReadInt(Encoding.UTF8.GetBytes("forsen"));
Assert.Equal(0, r2);
}
}
42 changes: 42 additions & 0 deletions MiniTwitch.Helix.Test/Converters/LongConverterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using MiniTwitch.Helix.Internal.Json;

namespace MiniTwitch.Helix.Test.Converters;

public class LongConverterTest
{
[Fact]
public void Read_Int()
{
var r = LongConverter.ReadLong(Encoding.UTF8.GetBytes("12345"));
Assert.Equal(12345, r);
}

[Fact]
public void Read_Long()
{
var r = LongConverter.ReadLong(Encoding.UTF8.GetBytes("8223372036854780000"));
Assert.Equal(8223372036854780000, r);
}

[Fact]
public void Read_Empty()
{
var r = LongConverter.ReadLong(Span<byte>.Empty);
Assert.Equal(0, r);
}

[Fact]
public void Read_Null()
{
var r2 = LongConverter.ReadLong(Encoding.UTF8.GetBytes("null"));
Assert.Equal(0, r2);
}

[Fact]
public void Read_Undefined()
{
var r2 = LongConverter.ReadLong(Encoding.UTF8.GetBytes("forsen"));
Assert.Equal(0, r2);
}
}
Loading

0 comments on commit 54e4c0d

Please sign in to comment.