### Install Go ArcTest Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This snippet shows the command to install the Go ArcTest library using the go get command. It's the initial step before using the library in your project. ```bash go get github.com/mstrYoda/go-arctest ``` -------------------------------- ### Comprehensive Go Architecture Test Example Source: https://context7.com/mstryoda/go-arctest/llms.txt This example demonstrates a full architecture test setup using Go ArcTest, including parsing packages, defining layers, establishing dependencies, and validating multiple architectural rules. It covers layered architecture checks, dependency rules, interface implementations, and method parameter rules. ```go func TestCompleteArchitecture(t *testing.T) { // Initialize architecture arch, err := arctest.New("./example_project") if err != nil { t.Fatalf("Failed to create architecture: %v", err) } // Parse all relevant packages err = arch.ParsePackages("domain", "application", "infrastructure", "presentation") if err != nil { t.Fatalf("Failed to parse packages: %v", err) } // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") infrastructureLayer, _ := arctest.NewLayer("Infrastructure", "^infrastructure$") presentationLayer, _ := arctest.NewLayer("Presentation", "^presentation$") // Create layered architecture layeredArch := arch.NewLayeredArchitecture( domainLayer, applicationLayer, infrastructureLayer, presentationLayer, ) // Define allowed dependencies (Clean Architecture style) applicationLayer.DependsOnLayer(domainLayer) infrastructureLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(applicationLayer) // Check layered architecture violations, err := layeredArch.Check() if err != nil { t.Fatalf("Failed to check layered architecture: %v", err) } for _, v := range violations { t.Errorf("Layer violation: %s", v) } // Rule 1: Domain should not depend on any other layer rule1, _ := arch.DoesNotDependOn("^domain$", "^(application|infrastructure|presentation)$") valid1, violations1 := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{rule1}) if !valid1 { for _, v := range violations1 { t.Errorf("Dependency violation: %s", v) } } // Rule 2: Infrastructure repositories should implement domain interfaces rule2, _ := arch.StructsImplementInterfaces(".*Repository$", ".*RepositoryInterface$") valid2, violations2 := arch.ValidateInterfaceImplementations([]*arctest.InterfaceImplementationRule{rule2}) if !valid2 { for _, v := range violations2 { t.Errorf("Interface violation: %s", v) } } // Rule 3: Services should use interfaces as parameters (Dependency Inversion) rule3, _ := arch.MethodsShouldUseInterfaceParameters(".*Service$", "New.*", ".*Repository$") valid3, violations3 := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule3}) if !valid3 { for _, v := range violations3 { t.Errorf("Parameter violation: %s", v) } } } ``` -------------------------------- ### Handle Nested Packages in Go Architecture Tests Source: https://github.com/mstryoda/go-arctest/blob/main/README.md Shows how go-arctest automatically handles nested packages when defining layers. Using a pattern like `^domain$` will include subpackages such as `domain/entities` and `domain/services`. The example demonstrates parsing nested packages and defining layers that match them, then checking the architecture. ```go // Initialize architecture arch, _ := arctest.New("./") // This will recursively parse the packages and their subpackages arch.ParsePackages("app", "domain") // Define layers - these patterns will match the packages and their subpackages appLayer, _ := arctest.NewLayer("App", "^app$") // Matches app, app/handlers, app/services, etc. domainLayer, _ := arctest.NewLayer("Domain", "^domain$") // Matches domain and all subpackages // Create a layered architecture layeredArch := arch.NewLayeredArchitecture(appLayer, domainLayer) // Define allowed dependencies appLayer.DependsOnLayer(domainLayer, layeredArch) // Run the check - this will check all packages and subpackages violations, _ := layeredArch.Check() // The result will include violations from all subpackages for _, violation := range violations { t.Errorf("Architecture violation: %s", violation) } ``` -------------------------------- ### Initialize Architecture Analysis in Go Source: https://context7.com/mstryoda/go-arctest/llms.txt Demonstrates how to initialize a new Architecture instance in Go using Go ArcTest. It shows parsing all packages or specific packages from a given base path. ```go package myproject import ( "testing" "github.com/mstrYoda/go-arctest/pkg/arctest" ) func TestArchitectureSetup(t *testing.T) { // Initialize architecture with project root arch, err := arctest.New("./") if err != nil { t.Fatalf("Failed to create architecture: %v", err) } // Parse all packages in the project automatically err = arch.ParsePackages() if err != nil { t.Fatalf("Failed to parse packages: %v", err) } // Or parse specific packages and their subpackages err = arch.ParsePackages("domain", "application", "infrastructure", "presentation") if err != nil { t.Fatalf("Failed to parse packages: %v", err) } } ``` -------------------------------- ### Initialize and Parse Architecture with Go ArcTest Source: https://github.com/mstryoda/go-arctest/blob/main/README.md Demonstrates how to initialize the Go ArcTest architecture object with the project root and then parse all packages within the project. Error handling is included for both initialization and parsing steps. ```go // Initialize architecture with project root arch, err := arctest.New("./") if err != nil { t.Fatalf("Failed to create architecture: %v", err) } // Parse all packages in the project err = arch.ParsePackages() if err != nil { t.Fatalf("Failed to parse packages: %v", err) } // Parse specific packages and their subpackages // err = arch.ParsePackages("internal/domain", "internal/service") ``` -------------------------------- ### Create Logical Layers for Architecture in Go Source: https://context7.com/mstryoda/go-arctest/llms.txt Illustrates the creation of new Layer instances in Go using Go ArcTest. Each layer represents a logical grouping of packages defined by regex patterns, including subpackages. ```go func TestLayerCreation(t *testing.T) { // Create layers with regex patterns // ^domain$ matches "domain" and all subpackages like "domain/entities", "domain/services" domainLayer, err := arctest.NewLayer("Domain", "^domain$") if err != nil { t.Fatalf("Failed to create domain layer: %v", err) } applicationLayer, err := arctest.NewLayer("Application", "^application$") if err != nil { t.Fatalf("Failed to create application layer: %v", err) } infrastructureLayer, err := arctest.NewLayer("Infrastructure", "^infrastructure$") if err != nil { t.Fatalf("Failed to create infrastructure layer: %v", err) } presentationLayer, err := arctest.NewLayer("Presentation", "^presentation$") if err != nil { t.Fatalf("Failed to create presentation layer: %v", err) } utilsLayer, err := arctest.NewLayer("Utils", "^utils$") if err != nil { t.Fatalf("Failed to create utils layer: %v", err) } } ``` -------------------------------- ### Validate Layered Architecture Dependencies in Go Source: https://context7.com/mstryoda/go-arctest/llms.txt Shows how to set up and check a layered architecture in Go using Go ArcTest. It involves creating layers, associating them with an architecture, defining allowed dependencies, and checking for violations. ```go func TestLayeredArchitecture(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application", "infrastructure", "presentation") // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") infrastructureLayer, _ := arctest.NewLayer("Infrastructure", "^infrastructure$") presentationLayer, _ := arctest.NewLayer("Presentation", "^presentation$") // Create layered architecture - this associates layers with the architecture layeredArch := arch.NewLayeredArchitecture( domainLayer, applicationLayer, infrastructureLayer, presentationLayer, ) // Define allowed dependency rules between layers applicationLayer.DependsOnLayer(domainLayer) infrastructureLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(applicationLayer) // Check for violations - any unlisted dependency is a violation violations, err := layeredArch.Check() if err != nil { t.Fatalf("Failed to check layered architecture: %v", err) } for _, violation := range violations { t.Errorf("Architecture violation: %s", violation) } } ``` -------------------------------- ### Define and Check Layered Architecture in Go Source: https://github.com/mstryoda/go-arctest/blob/main/README.md Illustrates how to define layers (Domain, Application, Infrastructure, Presentation) and specify dependency rules between them using the go-arctest library. It then checks the defined layered architecture for violations. ```go // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain/.*$") applicationLayer, _ := arctest.NewLayer("Application", "^application/.*$") infrastructureLayer, _ := arctest.NewLayer("Infrastructure", "^infrastructure/.*$") presentationLayer, _ := arctest.NewLayer("Presentation", "^presentation/.*$") // Define layered architecture layeredArch := arch.NewLayeredArchitecture( domainLayer, applicationLayer, infrastructureLayer, presentationLayer, ) // Define dependency rules applicationLayer.DependsOnLayer(domainLayer) infrastructureLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(domainLayer) presentationLayer.DependsOnLayer(applicationLayer) // Check layered architecture violations, err := layeredArch.Check() if err != nil { t.Fatalf("Failed to check layered architecture: %v", err) } for _, violation := range violations { t.Errorf("Architecture violation: %s", violation) } ``` -------------------------------- ### Define Direct Layer Dependency Rules in Go Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This Go code demonstrates how to define direct layer dependencies using a more intuitive API provided by go-arctest. It shows how to specify that one layer should not depend on another without using regular expressions, which is useful for layered architectures. ```go // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") utilsLayer, _ := arctest.NewLayer("Utils", "^utils$") // Set architecture for the layers layeredArch := arch.NewLayeredArchitecture(domainLayer, applicationLayer, utilsLayer) // Method 1: Define direct layer dependencies // Domain should not depend on Application layer domainAppRule, err := domainLayer.DoesNotDependOnLayer(applicationLayer) if err != nil { t.Fatalf("Failed to create layer dependency rule: %v", err) } // Domain should not depend on Utils layer domainUtilsRule, err := domainLayer.DoesNotDependOnLayer(utilsLayer) if err != nil { t.Fatalf("Failed to create layer dependency rule: %v", err) } // Test the rules valid, violations := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{ domainAppRule, domainUtilsRule, }) // Method 2: Define allowed dependencies between layers // Application layer can depend on Domain layer err = applicationLayer.DependsOnLayer(domainLayer, layeredArch) if err != nil { t.Fatalf("Failed to create layer dependency: %v", err) } // Check layered architecture for violations violations, err := layeredArch.Check() ``` -------------------------------- ### Test for Dependency Violations in Go Architecture Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This Go test function demonstrates how to use go-arctest to check for dependency violations within a project. It initializes the architecture, defines layers, creates a specific rule (e.g., domain should not depend on utils), validates dependencies, and asserts whether violations are detected as expected. ```go func TestDependencyViolation(t *testing.T) { // Initialize architecture and parse packages arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "utils") // domain should not depend on utils // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain$") utilsLayer, _ := arctest.NewLayer("Utils", "^utils$") // Create a rule that domain should not depend on utils layeredArch := arch.NewLayeredArchitecture(domainLayer, utilsLayer) // Create the rule using the layer-specific method rule, _ := domainLayer.DoesNotDependOn("^utils$") // Validate dependencies - we expect violations if domain imports utils valid, violations := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{rule}) // For a test that expects violations (like testing that our rules work) // we'd check that valid is false and violations contains expected issues if valid { t.Error("Expected dependency violations, but none were found!") } else { t.Logf("Successfully detected dependency violations:") for _, violation := range violations { t.Logf(" ✓ %s", violation) } } } ``` -------------------------------- ### Test Interface Parameter Violations in Go Source: https://github.com/mstryoda/go-arctest/blob/main/README.md Demonstrates how to create a test to verify methods use interfaces as parameters instead of concrete struct implementations, enforcing the Dependency Inversion Principle. It initializes the architecture, defines a rule for methods to use interface parameters, validates against this rule, and then uses the inverse rule to confirm concrete struct usage. ```go func TestInterfaceParameterViolation(t *testing.T) { // Initialize architecture and parse packages arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "utils") // Define the rule: methods named "Update*" should use interface parameters for "Logger" // This will check if methods are violating the rule by using concrete structs rule, _ := arch.MethodsShouldUseInterfaceParameters(".*Service.*", "Update.*", ".*Logger") // Validate parameter types - we expect violations valid, violations := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule}) // If we're testing that our architecture rule is correctly detecting violations, // we would expect valid to be false and violations to be non-empty if valid { t.Error("Expected interface parameter violations, but none were found!") } else { t.Logf("Successfully detected methods using concrete types instead of interfaces:") for _, violation := range violations { t.Logf(" ✓ %s", violation) } } // You can also use the inverse rule to confirm that concrete types are being used structRule, _ := arch.MethodsShouldUseStructParameters(".*Service.*", "Update.*", ".*Logger") structValid, _ := arch.ValidateMethodParameters([]*arctest.ParameterRule{structRule}) // This should pass as we expect to find methods using concrete structs if !structValid { t.Error("Methods are not using struct parameters as expected in our test case") } } ``` -------------------------------- ### Validate Interface Implementations with Go ArcTest Source: https://github.com/mstryoda/go-arctest/blob/main/README.md Shows how to create a rule in Go ArcTest to ensure that specific structs implement required interfaces. The code then validates these implementations and logs any discrepancies. ```go // Create a rule that all repository structs must implement repository interfaces rule, err := arch.StructsImplementInterfaces(".*Repository$", ".*RepositoryInterface$") if err != nil { t.Fatalf("Failed to create interface implementation rule: %v", err) } // Validate interface implementations valid, violations := arch.ValidateInterfaceImplementations([]*arctest.InterfaceImplementationRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Interface implementation violation: %s", violation) } } ``` -------------------------------- ### Check Package Dependencies with Go ArcTest Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This Go code snippet illustrates how to define a rule to check if one package should not depend on another using regular expressions. It then validates these dependencies and reports any violations. ```go // Create a rule that one package should not depend on another rule, err := arch.DoesNotDependOn("^domain/.*$", "^infrastructure/.*$") if err != nil { t.Fatalf("Failed to create dependency rule: %v", err) } // Validate dependencies valid, violations := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Dependency violation: %s", violation) } } ``` -------------------------------- ### Find All Interface Implementations (Go) Source: https://context7.com/mstryoda/go-arctest/llms.txt Finds all structs within a specified package that implement a given interface. This function is useful for discovering existing implementations or verifying that a specific interface is indeed implemented by the intended types. It returns a list of structs and an error if the search fails. ```go func TestFindImplementations(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "infrastructure") // Find all structs implementing UserRepositoryInterface in the domain package implementations, err := arch.FindAllImplementations("UserRepositoryInterface", "domain") if err != nil { t.Fatalf("Failed to find implementations: %v", err) } t.Logf("Found %d implementations:", len(implementations)) for _, impl := range implementations { t.Logf(" - %s in package %s", impl.Name, impl.Pkg.Path) } } ``` -------------------------------- ### Methods Should Use Interface Parameters Rule (Go) Source: https://context7.com/mstryoda/go-arctest/llms.txt Creates a rule to enforce the Dependency Inversion Principle by ensuring that methods use interface types as parameters instead of concrete struct types. This promotes loose coupling and testability. It takes regex patterns for the struct containing the method, the method name, and the interface type. ```go func TestInterfaceParameterEnforcement(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application", "utils") // Rule: Service methods should use interface parameters for repositories rule, err := arch.MethodsShouldUseInterfaceParameters(".*Service$", "New.*", ".*Repository$") if err != nil { t.Fatalf("Failed to create parameter rule: %v", err) } // Validate parameter types valid, violations := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Parameter type violation: %s", violation) // Output: Parameter type violation: Method "NewUserService" of struct "UserService" // in package "application" uses struct type "UserRepository" as parameter, // but should use an interface } } } ``` -------------------------------- ### Methods Should Use Struct Parameters Rule (Go) Source: https://context7.com/mstryoda/go-arctest/llms.txt Creates a rule to enforce that methods use concrete struct types as parameters, which is the inverse of the interface parameter rule. This can be useful for specific scenarios, such as testing or ensuring that certain low-level functions receive concrete types. It takes regex patterns for the struct, method, and parameter types. ```go func TestStructParameterEnforcement(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "utils") // Rule: Verify that methods use struct parameters (useful for testing violations) rule, err := arch.MethodsShouldUseStructParameters(".*Service.*", "Update.*", ".*Logger") if err != nil { t.Fatalf("Failed to create parameter rule: %v", err) } valid, violations := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule}) // If methods should be using structs but are using interfaces, this will fail if !valid { for _, v := range violations { t.Errorf("Expected struct parameter: %s", v) } } } ``` -------------------------------- ### Define Layer-Specific Architectural Rules in Go Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This snippet shows how to define architectural rules that apply to specific layers within a Go project using the go-arctest library. It covers defining layer dependencies and setting rules for layer interactions, such as non-dependencies and interface implementations. ```go // Define layer dependencies using layer-specific methods applicationLayer.DependsOn("Domain", layeredArch) infrastructureLayer.DependsOn("Domain", layeredArch) presentationLayer.DependsOn("Domain", layeredArch) presentationLayer.DependsOn("Application", layeredArch) // Layer-specific rules: // 1. Domain layer should not depend on any other layer rule1, err := domainLayer.DoesNotDependOn("^(application|infrastructure|presentation)$") // 2. Infrastructure repositories should implement domain interfaces rule2, err := infrastructureLayer.StructsImplementInterfaces(".*Repository$", ".*RepositoryInterface$") // 3. Application services should use interfaces as parameters rule3, err := applicationLayer.MethodsShouldUseInterfaceParameters(".*Service$", "New.*", ".*Repository$") // Run the tests with these layer-specific rules valid1, violations1 := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{rule1}) valid2, violations2 := arch.ValidateInterfaceImplementations([]*arctest.InterfaceImplementationRule{rule2}) valid3, violations3 := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule3}) ``` -------------------------------- ### Check Method Parameters for Interfaces with Go ArcTest Source: https://github.com/mstryoda/go-arctest/blob/main/README.md This Go code snippet demonstrates how to enforce a rule that service methods should use interfaces instead of concrete struct types for their parameters. It validates these parameter types and reports violations. ```go // Create a rule that all service methods should use interfaces as parameters rule, err := arch.MethodsShouldUseInterfaceParameters(".*Service$", ".*", ".*Repository$") if err != nil { t.Fatalf("Failed to create parameter rule: %v", err) } // Validate parameter types valid, violations := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Parameter type violation: %s", violation) } } ``` -------------------------------- ### Create Package Dependency Rule: DoesNotDependOn Source: https://context7.com/mstryoda/go-arctest/llms.txt Defines a rule where packages matching a source pattern must not import packages matching a target pattern. This function is useful for enforcing architectural boundaries between different parts of a Go project using regular expressions. ```go func TestPackageDependencyRules(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application", "infrastructure", "presentation") // Domain layer should not depend on other layers using regex patterns rule, err := arch.DoesNotDependOn("^domain.*$", "^(application|infrastructure|presentation).*$") if err != nil { t.Fatalf("Failed to create dependency rule: %v", err) } // Validate dependencies against the rule valid, violations := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Dependency violation: %s", violation) // Output: Dependency violation: Package "domain" imports "infrastructure/ப்பின", // but this is not allowed by rule: ^domain.*$ cannot import ^(application|infrastructure|presentation).*$ } } } ``` -------------------------------- ### Structs Implement Interfaces Rule (Go) Source: https://context7.com/mstryoda/go-arctest/llms.txt Creates a rule to ensure that structs matching a specific pattern implement interfaces matching another pattern. This is useful for enforcing architectural conventions where certain types of structs must adhere to specific interface contracts. It takes two regex patterns as input: one for struct names and one for interface names. ```go func TestInterfaceImplementations(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "infrastructure") // Rule: All repository structs must implement repository interfaces rule, err := arch.StructsImplementInterfaces(".*Repository$", ".*RepositoryInterface$") if err != nil { t.Fatalf("Failed to create interface implementation rule: %v", err) } // Validate interface implementations valid, violations := arch.ValidateInterfaceImplementations([]*arctest.InterfaceImplementationRule{rule}) if !valid { for _, violation := range violations { t.Errorf("Interface implementation violation: %s", violation) // Output: Interface implementation violation: Struct "UserRepository" in package // "infrastructure" does not implement any interface matching ".*RepositoryInterface$" } } } ``` -------------------------------- ### Define Allowed Layer Dependencies: DependsOn / DependsOnLayer Source: https://context7.com/mstryoda/go-arctest/llms.txt Specifies the allowed direct dependencies between layers, either by using layer names as strings or by referencing layer objects directly. This helps in defining and enforcing a clear hierarchical structure within the application. ```go func TestAllowedDependencies(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application", "infrastructure", "presentation", "utils") domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") infrastructureLayer, _ := arctest.NewLayer("Infrastructure", "^infrastructure$") presentationLayer, _ := arctest.NewLayer("Presentation", "^presentation$") utilsLayer, _ := arctest.NewLayer("Utils", "^utils$") layeredArch := arch.NewLayeredArchitecture( domainLayer, applicationLayer, infrastructureLayer, presentationLayer, utilsLayer, ) // Method 1: Using layer names (string-based) applicationLayer.DependsOn("Domain") applicationLayer.DependsOn("Utils") infrastructureLayer.DependsOn("Domain") presentationLayer.DependsOn("Domain") presentationLayer.DependsOn("Application") // Method 2: Using direct layer references err := infrastructureLayer.DependsOnLayer(utilsLayer) if err != nil { t.Fatalf("Failed to create layer dependency: %v", err) } // Check for violations - Domain importing Utils is NOT allowed (not defined above) violations, err := layeredArch.Check() if err != nil { t.Fatalf("Failed to check layered architecture: %v", err) } if len(violations) > 0 { t.Logf("Architecture violations detected:") for _, v := range violations { t.Logf(" - %s", v) } } } ``` -------------------------------- ### Layer-Scoped Structs Implement Interfaces Rule (Go) Source: https://context7.com/mstryoda/go-arctest/llms.txt Creates a rule scoped to a specific layer, ensuring structs within that layer implement interfaces matching a given pattern. This allows for more granular architectural enforcement within different layers of an application. It requires defining layers first and then applying the rule to a specific layer object. ```go func TestLayerScopedInterfaceRules(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "infrastructure") domainLayer, _ := arctest.NewLayer("Domain", ".^domain$") infrastructureLayer, _ := arctest.NewLayer("Infrastructure", ".^infrastructure$") arch.NewLayeredArchitecture(domainLayer, infrastructureLayer) // Infrastructure repositories should implement domain interfaces rule, err := infrastructureLayer.StructsImplementInterfaces(".*Repository$", ".*RepositoryInterface$") if err != nil { t.Fatalf("Failed to create interface implementation rule: %v", err) } valid, violations := arch.ValidateInterfaceImplementations([]*arctest.InterfaceImplementationRule{rule}) if !valid { for _, v := range violations { t.Errorf("Violation: %s", v) } } } ``` -------------------------------- ### Enforce Interface Parameters for Methods in Go Source: https://context7.com/mstryoda/go-arctest/llms.txt This rule ensures that methods within a specified layer use interface parameters, promoting the Dependency Inversion Principle. It targets methods matching a given name pattern and checks their parameters against a repository pattern. Dependencies include the `arctest` library. ```go func TestLayerScopedParameterRules(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application") domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") arch.NewLayeredArchitecture(domainLayer, applicationLayer) // Application services should use interfaces as parameters rule, err := applicationLayer.MethodsShouldUseInterfaceParameters(".*Service$", "New.*", ".*Repository$") if err != nil { t.Fatalf("Failed to create parameter rule: %v", err) } valid, violations := arch.ValidateMethodParameters([]*arctest.ParameterRule{rule}) if !valid { for _, v := range violations { t.Errorf("Violation: %s", v) } } } ``` -------------------------------- ### Create Layer Dependency Rule: DoesNotDependOnLayer Source: https://context7.com/mstryoda/go-arctest/llms.txt Establishes a rule preventing one defined layer from directly depending on another. This method simplifies dependency management by allowing direct layer references instead of relying on complex regex patterns. ```go func TestLayerDependencyRules(t *testing.T) { arch, _ := arctest.New("./example_project") arch.ParsePackages("domain", "application", "utils") // Define layers domainLayer, _ := arctest.NewLayer("Domain", "^domain$") applicationLayer, _ := arctest.NewLayer("Application", "^application$") utilsLayer, _ := arctest.NewLayer("Utils", "^utils$") // Create layered architecture to associate layers arch.NewLayeredArchitecture(domainLayer, applicationLayer, utilsLayer) // Create rules using direct layer references (no regex needed) domainAppRule, err := domainLayer.DoesNotDependOnLayer(applicationLayer) if err != nil { t.Fatalf("Failed to create layer dependency rule: %v", err) } domainUtilsRule, err := domainLayer.DoesNotDependOnLayer(utilsLayer) if err != nil { t.Fatalf("Failed to create layer dependency rule: %v", err) } // Validate multiple rules at once valid, violations := arch.ValidateDependenciesWithRules([]*arctest.DependencyRule{ domainAppRule, domainUtilsRule, }) if !valid { t.Logf("Detected dependency violations:") for _, violation := range violations { t.Logf(" - %s", violation) } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.