<commit_before>﻿Imports NUnit.Framework
Imports DotNetNuke.Collections
Imports DotNetNuke.UI.Modules
Imports DotNetNuke.Entities.Controllers
Imports NSubstitute

<TestFixture>
Public Class SettingsRepositoryTests
    <Test>
    Public Sub GetValue_GivenCustomConverter_ConvertsSettingValue()
        StubHostSettings(New Dictionary(Of String, String) From {{"MyModule_TheSetting", "yup"}})
        Dim setting As New Setting(Of Boolean)("MyModule_TheSetting", SettingScope.Host, False)
        Dim repository As ISettingsRepository = New SettingsRepository(New ModuleInstanceContext())

        Dim settingValue = repository.GetValue(setting, CollectionExtensions.GetFlexibleBooleanParsingFunction("yup"))

        Assert.IsTrue(settingValue)
    End Sub

    Private Sub StubHostSettings(settings As Dictionary(Of String, String))
        Dim stubHostController = Substitute.For(Of IHostController)()
        HostController.RegisterInstance(stubHostController)
        stubHostController = HostController.Instance
        stubHostController.GetSettingsDictionary().Returns(settings)
    End Sub
End Class<commit_msg>Add tests for various scenarios getting Boolean values<commit_after>﻿Imports NUnit.Framework
Imports DotNetNuke.Collections
Imports DotNetNuke.UI.Modules
Imports DotNetNuke.Entities.Controllers
Imports NSubstitute

<TestFixture>
Public Class SettingsRepositoryTests
    <Test>
    Public Sub GetValue_GivenCustomConverter_ConvertsSettingValue()
        StubHostSettings(New Dictionary(Of String, String) From {{"MyModule_TheSetting", "yup"}})
        Dim setting As New Setting(Of Boolean)("MyModule_TheSetting", SettingScope.Host, False)
        Dim repository As ISettingsRepository = New SettingsRepository(New ModuleInstanceContext())

        Dim settingValue = repository.GetValue(setting, CollectionExtensions.GetFlexibleBooleanParsingFunction("yup"))

        Assert.IsTrue(settingValue)
    End Sub

    <Test>
    <TestCase("true", True)>
    <TestCase("false", False)>
    Public Sub GetValue_GivenStandardBooleanTrueText_ConvertsSettingValue(settingText As String, expectedValue As Boolean)
        StubHostSettings(New Dictionary(Of String, String) From {{"MyModule_TheSetting", settingText}})
        Dim setting As New Setting(Of Boolean)("MyModule_TheSetting", SettingScope.Host, False)
        Dim repository As ISettingsRepository = New SettingsRepository(New ModuleInstanceContext())

        Dim settingValue = repository.GetValue(setting)

        Assert.AreEqual(expectedValue, settingValue)
    End Sub

    <Test>
    Public Sub GetValue_GivenNoMatchingSetting_ReturnsDefaultSettingValue()
        StubHostSettings(New Dictionary(Of String, String) From {{"MyModule_OtherSetting", "something"}})
        Dim setting As New Setting(Of Boolean)("MyModule_TheSetting", SettingScope.Host, True)
        Dim repository As ISettingsRepository = New SettingsRepository(New ModuleInstanceContext())

        Dim settingValue = repository.GetValue(setting)

        Assert.IsTrue(settingValue)
    End Sub

    <Test>
    Public Sub GetValue_GivenInvalidBooleanText_ThrowsFormatException()
        StubHostSettings(New Dictionary(Of String, String) From {{"MyModule_TheSetting", "not a boolean"}})
        Dim setting As New Setting(Of Boolean)("MyModule_TheSetting", SettingScope.Host, False)
        Dim repository As ISettingsRepository = New SettingsRepository(New ModuleInstanceContext())

        Assert.Throws(Of FormatException)(Function() repository.GetValue(setting))
    End Sub

    Private Sub StubHostSettings(settings As Dictionary(Of String, String))
        Dim stubHostController = Substitute.For(Of IHostController)()
        HostController.RegisterInstance(stubHostController)
        stubHostController = HostController.Instance
        stubHostController.GetSettingsDictionary().Returns(settings)
    End Sub
End Class
{'fraction_non_alphanumeric': 0.07541322314049587, 'fraction_numerical': 0.0, 'mean_word_length': 4.931087289433385, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7554557', 'n_tokens_mistral': 994, 'n_tokens_neox': 968, 'n_words': 245}