From da8494d3e23502b69bf136d6cc6ad3f0ba199812 Mon Sep 17 00:00:00 2001 From: Aleksey Kulikov Date: Sat, 21 Aug 2021 11:25:06 +0300 Subject: [PATCH] refactor(config): simplify api methods names --- example/config_example.dart | 14 ++++---- lib/src/bindings/config.dart | 6 ++-- lib/src/config.dart | 23 +++++-------- test/config_test.dart | 64 +++++++++++++++++------------------- test/repository_test.dart | 2 +- 5 files changed, 50 insertions(+), 59 deletions(-) diff --git a/example/config_example.dart b/example/config_example.dart index 05f2c17..05dd29e 100644 --- a/example/config_example.dart +++ b/example/config_example.dart @@ -13,8 +13,7 @@ void main() async { final config = Config.open(); print('All entries of system/global config:'); - final entries = config.getEntries(); - for (final entry in entries.entries) { + for (final entry in config.variables.entries) { print('${entry.key}: ${entry.value}'); } // free() should be called on object to free memory when done. @@ -26,18 +25,17 @@ void main() async { final repoConfig = Config.open('$tmpDir/.git/config'); print('\nAll entries of repo config:'); - final entries = repoConfig.getEntries(); - for (final entry in entries.entries) { + for (final entry in repoConfig.variables.entries) { print('${entry.key}: ${entry.value}'); } // Set value of config variable - repoConfig.setValue('core.variable', 'value'); + repoConfig['core.variable'] = 'value'; print( - '\nNew value for variable "core.variable": ${repoConfig.getValue('core.variable')}'); + '\nNew value for variable "core.variable": ${repoConfig['core.variable']}'); // Delete variable - repoConfig.deleteEntry('core.variable'); + repoConfig.delete('core.variable'); repoConfig.free(); } catch (e) { @@ -50,7 +48,7 @@ void main() async { final globalConfig = Config.global(); // Get value of config variable. - final userName = globalConfig.getValue('user.name'); + final userName = globalConfig['user.name']; print('\nUser Name from global config: $userName'); globalConfig.free(); diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 0c4d57e..e0dd332 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -222,7 +222,7 @@ Map getEntries(Pointer cfg) { /// (usually the local one). /// /// Throws a [LibGit2Error] if error occured. -void deleteEntry(Pointer cfg, String variable) { +void delete(Pointer cfg, String variable) { final name = variable.toNativeUtf8().cast(); final error = libgit2.git_config_delete_entry(cfg, name); calloc.free(name); @@ -236,7 +236,7 @@ void deleteEntry(Pointer cfg, String variable) { /// /// If regexp is present, then the iterator will only iterate over all /// values which match the pattern. -List getMultivarValue( +List multivarValues( Pointer cfg, String variable, String? regexp, @@ -270,7 +270,7 @@ List getMultivarValue( /// highest level (usually the local one). /// /// The regexp is applied case-sensitively on the value. -void setMultivarValue( +void setMultivar( Pointer cfg, String variable, String regexp, diff --git a/lib/src/config.dart b/lib/src/config.dart index 79ec674..e2e2814 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -84,17 +84,14 @@ class Config { Config get snapshot => Config(bindings.snapshot(_configPointer)); /// Returns map of all the config variables and their values. - Map getEntries() { - return bindings.getEntries(_configPointer); - } + Map get variables => bindings.getEntries(_configPointer); /// Returns the value of config [variable]. - String getValue(String variable) { - return bindings.getValue(_configPointer, variable); - } + String operator [](String variable) => + bindings.getValue(_configPointer, variable); /// Sets the [value] of config [variable]. - void setValue(String variable, dynamic value) { + void operator []=(String variable, dynamic value) { if (value.runtimeType == bool) { bindings.setBool(_configPointer, variable, value); } else if (value.runtimeType == int) { @@ -108,16 +105,14 @@ class Config { /// (usually the local one). /// /// Throws a [LibGit2Error] if error occured. - void deleteEntry(String variable) { - bindings.deleteEntry(_configPointer, variable); - } + void delete(String variable) => bindings.delete(_configPointer, variable); /// Returns list of values for multivar [variable] /// /// If [regexp] is present, then the iterator will only iterate over all /// values which match the pattern. - List getMultivarValue(String variable, {String? regexp}) { - return bindings.getMultivarValue(_configPointer, variable, regexp); + List multivar(String variable, {String? regexp}) { + return bindings.multivarValues(_configPointer, variable, regexp); } /// Sets the [value] of a multivar [variable] in the config file with the @@ -125,8 +120,8 @@ class Config { /// /// The [regexp] is applied case-sensitively on the value. /// Empty [regexp] sets [value] for all values of a multivar [variable]. - void setMultivarValue(String variable, String regexp, String value) { - bindings.setMultivarValue(_configPointer, variable, regexp, value); + void setMultivar(String variable, String regexp, String value) { + bindings.setMultivar(_configPointer, variable, regexp, value); } /// Deletes one or several values from a multivar [variable] in the config file diff --git a/test/config_test.dart b/test/config_test.dart index 9fe02b0..93e7e5a 100644 --- a/test/config_test.dart +++ b/test/config_test.dart @@ -34,61 +34,59 @@ void main() { expect(config, isA()); }); - test('getEntries() returns map with variables and values', () { - final entries = config.getEntries(); - expect(entries['remote.origin.url'], equals('someurl')); + test('returns map with variables and values', () { + expect(config.variables['remote.origin.url'], equals('someurl')); }); - group('getValue()', () { + group('get value', () { test('returns value of variable', () { - expect(config.getValue('core.bare'), equals('false')); + expect(config['core.bare'], equals('false')); }); test('throws when variable isn\'t found', () { expect( - () => config.getValue('not.there'), + () => config['not.there'], throwsA(isA()), ); }); }); - group('setValue()', () { + group('set value', () { test('sets boolean value for provided variable', () { - config.setValue('core.bare', true); - expect(config.getValue('core.bare'), equals('true')); + config['core.bare'] = true; + expect(config['core.bare'], equals('true')); }); test('sets integer value for provided variable', () { - config.setValue('core.repositoryformatversion', 1); - expect(config.getValue('core.repositoryformatversion'), equals('1')); + config['core.repositoryformatversion'] = 1; + expect(config['core.repositoryformatversion'], equals('1')); }); test('sets string value for provided variable', () { - config.setValue('remote.origin.url', 'updated'); - expect(config.getValue('remote.origin.url'), equals('updated')); + config['remote.origin.url'] = 'updated'; + expect(config['remote.origin.url'], equals('updated')); }); }); - group('deleteEntry()', () { + group('delete', () { test('successfully deletes entry', () { - expect(config.getValue('core.bare'), equals('false')); - config.deleteEntry('core.bare'); - final entries = config.getEntries(); - expect(entries['core.bare'], isNull); + expect(config['core.bare'], equals('false')); + config.delete('core.bare'); + expect(config.variables['core.bare'], isNull); }); test('throws on deleting non existing variable', () { expect( - () => config.deleteEntry('not.there'), + () => config.delete('not.there'), throwsA(isA()), ); }); }); - group('getMultivarValue()', () { - test('returns values of multivar', () { + group('get multivar values', () { + test('returns list of values', () { expect( - config.getMultivarValue('core.gitproxy'), + config.multivar('core.gitproxy'), [ 'proxy-command for kernel.org', 'default-proxy', @@ -96,29 +94,29 @@ void main() { ); }); - test('returns values of multivar for provided regexp', () { + test('returns list of values for provided regexp', () { expect( - config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), + config.multivar('core.gitproxy', regexp: 'for kernel.org\$'), ['proxy-command for kernel.org'], ); }); test('returns empty list if multivar not found', () { - expect(config.getMultivarValue('not.there'), equals([])); + expect(config.multivar('not.there'), equals([])); }); }); group('setMultivarValue()', () { test('sets value of multivar', () { - config.setMultivarValue('core.gitproxy', 'default', 'updated'); - final multivarValues = config.getMultivarValue('core.gitproxy'); + config.setMultivar('core.gitproxy', 'default', 'updated'); + final multivarValues = config.multivar('core.gitproxy'); expect(multivarValues, isNot(contains('default-proxy'))); expect(multivarValues, contains('updated')); }); test('sets value for all multivar values when regexp is empty', () { - config.setMultivarValue('core.gitproxy', '', 'updated'); - final multivarValues = config.getMultivarValue('core.gitproxy'); + config.setMultivar('core.gitproxy', '', 'updated'); + final multivarValues = config.multivar('core.gitproxy'); expect(multivarValues, isNot(contains('default-proxy'))); expect(multivarValues, isNot(contains('proxy-command for kernel.org'))); expect(multivarValues, contains('updated')); @@ -129,14 +127,14 @@ void main() { group('deleteMultivar()', () { test('successfully deletes value of a multivar', () { expect( - config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), + config.multivar('core.gitproxy', regexp: 'for kernel.org\$'), ['proxy-command for kernel.org'], ); config.deleteMultivar('core.gitproxy', 'for kernel.org\$'); expect( - config.getMultivarValue('core.gitproxy', regexp: 'for kernel.org\$'), + config.multivar('core.gitproxy', regexp: 'for kernel.org\$'), [], ); }); @@ -144,7 +142,7 @@ void main() { test('successfully deletes all values of a multivar when regexp is empty', () { expect( - config.getMultivarValue('core.gitproxy'), + config.multivar('core.gitproxy'), [ 'proxy-command for kernel.org', 'default-proxy', @@ -153,7 +151,7 @@ void main() { config.deleteMultivar('core.gitproxy', ''); - expect(config.getMultivarValue('core.gitproxy'), []); + expect(config.multivar('core.gitproxy'), []); }); }); }); diff --git a/test/repository_test.dart b/test/repository_test.dart index beff60c..a3cd248 100644 --- a/test/repository_test.dart +++ b/test/repository_test.dart @@ -143,7 +143,7 @@ void main() { test('returns config for repository', () { final config = repo.config; - expect(config.getValue('remote.origin.url'), + expect(config['remote.origin.url'], 'git://github.com/SkinnyMind/libgit2dart.git'); config.free();