Skip to content
Snippets Groups Projects
Select Git revision
  • 4ef1cd20ad417c5d6c60aff8c50b23b07e6c8df9
  • main default protected
  • refactor/pin-new-value
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • revert-37021-fix/36927-maven-tags
  • fix/user-agent
  • feat/37531-npm-install-twice
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/markdown/linking
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • 41.76.0
  • 41.75.0
  • 41.74.5
  • 41.74.4
  • 41.74.3
  • 41.74.2
  • 41.74.1
  • 41.74.0
  • 41.73.4
  • 41.73.3
  • 41.73.2
  • 41.73.1
  • 41.73.0
  • 41.72.1
  • 41.72.0
  • 42.0.0-next.2
  • 42.0.0-next.1
  • 41.71.1
  • 41.71.0
  • 41.70.3
41 results

generic.spec.ts

Blame
  • generic.spec.ts 4.60 KiB
    import { GenericVersion, GenericVersioningApi } from './generic';
    
    describe('modules/versioning/generic', () => {
      const optionalFunctions = [
        'isLessThanRange',
        'valueToVersion',
        'constructor',
        'hasOwnProperty',
        'isPrototypeOf',
        'propertyIsEnumerable',
        'should',
        'toLocaleString',
        'toString',
        'valueOf',
      ];
      function getAllPropertyNames(obj: any): string[] {
        const props: string[] = [];
        let o = obj;
    
        do {
          Object.getOwnPropertyNames(o).forEach((prop) => {
            if (!props.includes(prop)) {
              props.push(prop);
            }
          });
        } while ((o = Object.getPrototypeOf(o)));
    
        return props;
      }
    
      describe('GenericVersioningApi', () => {
        class DummyScheme extends GenericVersioningApi {
          protected override _compare(_version: string, _other: string): number {
            return _version ? _version.localeCompare(_other) : 0;
          }
    
          protected _parse(_version: string): GenericVersion | null {
            const matchGroups = _version.match(
              /^(?<major>\d)\.(?<minor>\d)\.(?<patch>\d)$/
            )?.groups;
            if (!matchGroups) {
              return null;
            }
            const { major, minor, patch } = matchGroups;
            return { release: [major, minor, patch].map((n) => parseInt(n, 10)) };
          }
        }
    
        const api = new DummyScheme();
    
        it('Scheme keys', () => {
          const schemeKeys = getAllPropertyNames(api)
            .filter(
              (val) => !optionalFunctions.includes(val) && !val.startsWith('_')
            )
            .filter(
              (val) =>
                !['minSatisfyingVersion', 'getSatisfyingVersion'].includes(val)
            )
            .sort();
          expect(schemeKeys).toEqual([
            'equals',
            'getMajor',
            'getMinor',
            'getNewValue',
            'getPatch',
            'isCompatible',
            'isGreaterThan',
            'isSingleVersion',
            'isStable',
            'isValid',
            'isVersion',
            'matches',
            'sortVersions',
          ]);
        });
    
        it('equals', () => {
          expect(api.equals('1.2.3', '1.2.3')).toBeTrue();
          expect(api.equals('1.2.3', '3.2.1')).toBeFalse();
        });
    
        it('getMajor', () => {
          expect(api.getMajor('4.5.6')).toBe(4);
          expect(api.getMajor('invalid')).toBeNull();
        });
    
        it('getMinor', () => {
          expect(api.getMinor('4.5.6')).toBe(5);
          expect(api.getMinor('invalid')).toBeNull();
        });
    
        it('getPatch', () => {
          expect(api.getPatch('4.5.6')).toBe(6);
          expect(api.getPatch('invalid')).toBeNull();
        });
    
        it('getNewValue', () => {
          expect(
            api.getNewValue({
              currentValue: '1.2.3',
              rangeStrategy: 'auto',
              currentVersion: '1.2.3',
              newVersion: '3.2.1',
            })
          ).toBe('3.2.1');
        });
    
        it('isCompatible', () => {
          expect(api.isCompatible('1.2.3', '')).toBe(true);
        });
    
        it('isGreaterThan', () => {
          expect(api.isGreaterThan('1.2.3', '3.2.1')).toBe(false);
          expect(api.isGreaterThan('3.2.1', '1.2.3')).toBe(true);
        });
    
        it('isSingleVersion', () => {
          expect(api.isSingleVersion('1.2.3')).toBe(true);
        });
    
        it('isStable', () => {
          expect(api.isStable('1.2.3')).toBe(true);
        });
    
        it('isValid', () => {
          expect(api.isValid('1.2.3')).toBe(true);
          expect(api.isValid('invalid')).toBe(false);
        });
    
        it('isVersion', () => {
          expect(api.isVersion('1.2.3')).toBe(true);
          expect(api.isVersion('invalid')).toBe(false);
        });
    
        it('matches', () => {
          expect(api.matches('1.2.3', '1.2.3')).toBe(true);
          expect(api.matches('1.2.3', '3.2.1')).toBe(false);
        });
    
        it('sortVersions', () => {
          expect(api.sortVersions('1.2.3', '1.2.3')).toBe(0);
          expect(api.sortVersions('1.2.3', '3.2.1')).toBe(-1);
          expect(api.sortVersions('3.2.1', '1.2.3')).toBe(1);
        });
    
        it('isLessThanRange', () => {
          expect(api.isLessThanRange('1.2.3', '3.2.1')).toBeTrue();
          expect(api.isLessThanRange('3.2.1', '1.2.3')).toBeFalse();
        });
    
        it('minSatisfyingVersion', () => {
          expect(api.minSatisfyingVersion(['1.2.3'], '1.2.3')).toBe('1.2.3');
          expect(
            api.minSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '2.2.2')
          ).toBe('2.2.2');
          expect(
            api.minSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '1.2.3')
          ).toBeNull();
        });
    
        it('getSatisfyingVersion', () => {
          expect(api.getSatisfyingVersion(['1.2.3'], '1.2.3')).toBe('1.2.3');
          expect(
            api.getSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '2.2.2')
          ).toBe('2.2.2');
          expect(
            api.getSatisfyingVersion(['1.1.1', '2.2.2', '3.3.3'], '1.2.3')
          ).toBeNull();
        });
      });
    });