#!/usr/local/cpanel/3rdparty/bin/perl -w
#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use cPstrict;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;
use Test::MockFile   qw/strict/;

use Test::Cpanel::Policy testing => 'Common::Plugins::LoadRun';

use Common::Plugins::LoadRun ();

subtest 'Test when module cannot be loaded' => sub {
    my $exception = dies { Common::Plugins::LoadRun::run_from_module( 'Does::Not::Exist', 'does_not_exist' ) };

    like(
        $exception,
        qr/Could not locate the module .*/,
        "Got exception when module doesn't exist"
    );
};

subtest 'Test when method cannot be loaded' => sub {
    my $run = Common::Plugins::LoadRun::run_from_module( 'Common::Plugins::Setup', 'does_not_exist' );

    is $run, undef, 'return undef if module exists but method not found';
};

subtest 'Test loading method from module' => sub {
    my $run = Common::Plugins::LoadRun::run_from_module( 'Common::Plugins::Setup', '_validate_fn_name', 'enable' );

    is $run, 1, 'run coderef and pass args to method';
};

done_testing;
